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