Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 1 | #include <linux/export.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 2 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 3 | #include <linux/sched/task.h> |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 4 | #include <linux/fs.h> |
| 5 | #include <linux/path.h> |
| 6 | #include <linux/slab.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 7 | #include <linux/fs_struct.h> |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 8 | #include "internal.h" |
| 9 | |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 10 | /* |
| 11 | * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. |
| 12 | * It can block. |
| 13 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 14 | void set_fs_root(struct fs_struct *fs, const struct path *path) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 15 | { |
| 16 | struct path old_root; |
| 17 | |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 18 | path_get(path); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 19 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 20 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 21 | old_root = fs->root; |
| 22 | fs->root = *path; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 23 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 24 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 25 | if (old_root.dentry) |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 26 | path_put(&old_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. |
| 31 | * It can block. |
| 32 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 33 | void set_fs_pwd(struct fs_struct *fs, const struct path *path) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 34 | { |
| 35 | struct path old_pwd; |
| 36 | |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 37 | path_get(path); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 38 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 39 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 40 | old_pwd = fs->pwd; |
| 41 | fs->pwd = *path; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 42 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 43 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 44 | |
| 45 | if (old_pwd.dentry) |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 46 | path_put(&old_pwd); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Al Viro | 82234e6 | 2012-03-15 14:48:55 -0400 | [diff] [blame] | 49 | static inline int replace_path(struct path *p, const struct path *old, const struct path *new) |
| 50 | { |
| 51 | if (likely(p->dentry != old->dentry || p->mnt != old->mnt)) |
| 52 | return 0; |
| 53 | *p = *new; |
| 54 | return 1; |
| 55 | } |
| 56 | |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 57 | void chroot_fs_refs(const struct path *old_root, const struct path *new_root) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 58 | { |
| 59 | struct task_struct *g, *p; |
| 60 | struct fs_struct *fs; |
| 61 | int count = 0; |
| 62 | |
| 63 | read_lock(&tasklist_lock); |
| 64 | do_each_thread(g, p) { |
| 65 | task_lock(p); |
| 66 | fs = p->fs; |
| 67 | if (fs) { |
Al Viro | 82234e6 | 2012-03-15 14:48:55 -0400 | [diff] [blame] | 68 | int hits = 0; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 69 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 70 | write_seqcount_begin(&fs->seq); |
Al Viro | 82234e6 | 2012-03-15 14:48:55 -0400 | [diff] [blame] | 71 | hits += replace_path(&fs->root, old_root, new_root); |
| 72 | hits += replace_path(&fs->pwd, old_root, new_root); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 73 | write_seqcount_end(&fs->seq); |
Al Viro | 82234e6 | 2012-03-15 14:48:55 -0400 | [diff] [blame] | 74 | while (hits--) { |
| 75 | count++; |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 76 | path_get(new_root); |
Al Viro | 82234e6 | 2012-03-15 14:48:55 -0400 | [diff] [blame] | 77 | } |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 78 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 79 | } |
| 80 | task_unlock(p); |
| 81 | } while_each_thread(g, p); |
| 82 | read_unlock(&tasklist_lock); |
| 83 | while (count--) |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 84 | path_put(old_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 87 | void free_fs_struct(struct fs_struct *fs) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 88 | { |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 89 | path_put(&fs->root); |
| 90 | path_put(&fs->pwd); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 91 | kmem_cache_free(fs_cachep, fs); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void exit_fs(struct task_struct *tsk) |
| 95 | { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 96 | struct fs_struct *fs = tsk->fs; |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 97 | |
| 98 | if (fs) { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 99 | int kill; |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 100 | task_lock(tsk); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 101 | spin_lock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 102 | tsk->fs = NULL; |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 103 | kill = !--fs->users; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 104 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 105 | task_unlock(tsk); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 106 | if (kill) |
| 107 | free_fs_struct(fs); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | struct fs_struct *copy_fs_struct(struct fs_struct *old) |
| 112 | { |
| 113 | struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL); |
| 114 | /* We don't need to lock fs - think why ;-) */ |
| 115 | if (fs) { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 116 | fs->users = 1; |
| 117 | fs->in_exec = 0; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 118 | spin_lock_init(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 119 | seqcount_init(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 120 | fs->umask = old->umask; |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 121 | |
| 122 | spin_lock(&old->lock); |
| 123 | fs->root = old->root; |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 124 | path_get(&fs->root); |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 125 | fs->pwd = old->pwd; |
Al Viro | f7a99c5 | 2012-06-09 00:59:08 -0400 | [diff] [blame] | 126 | path_get(&fs->pwd); |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 127 | spin_unlock(&old->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 128 | } |
| 129 | return fs; |
| 130 | } |
| 131 | |
| 132 | int unshare_fs_struct(void) |
| 133 | { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 134 | struct fs_struct *fs = current->fs; |
| 135 | struct fs_struct *new_fs = copy_fs_struct(fs); |
| 136 | int kill; |
| 137 | |
| 138 | if (!new_fs) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 139 | return -ENOMEM; |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 140 | |
| 141 | task_lock(current); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 142 | spin_lock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 143 | kill = !--fs->users; |
| 144 | current->fs = new_fs; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 145 | spin_unlock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 146 | task_unlock(current); |
| 147 | |
| 148 | if (kill) |
| 149 | free_fs_struct(fs); |
| 150 | |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(unshare_fs_struct); |
| 154 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 155 | int current_umask(void) |
| 156 | { |
| 157 | return current->fs->umask; |
| 158 | } |
| 159 | EXPORT_SYMBOL(current_umask); |
| 160 | |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 161 | /* to be mentioned only in INIT_TASK */ |
| 162 | struct fs_struct init_fs = { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 163 | .users = 1, |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 164 | .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock), |
John Stultz | 1ca7d67 | 2013-10-07 15:51:59 -0700 | [diff] [blame] | 165 | .seq = SEQCNT_ZERO(init_fs.seq), |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 166 | .umask = 0022, |
| 167 | }; |