]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
630d9c47 | 2 | #include <linux/export.h> |
3f07c014 | 3 | #include <linux/sched/signal.h> |
29930025 | 4 | #include <linux/sched/task.h> |
3e93cd67 AV |
5 | #include <linux/fs.h> |
6 | #include <linux/path.h> | |
7 | #include <linux/slab.h> | |
5ad4e53b | 8 | #include <linux/fs_struct.h> |
f03c6599 AV |
9 | #include "internal.h" |
10 | ||
3e93cd67 AV |
11 | /* |
12 | * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. | |
13 | * It can block. | |
14 | */ | |
dcf787f3 | 15 | void set_fs_root(struct fs_struct *fs, const struct path *path) |
3e93cd67 AV |
16 | { |
17 | struct path old_root; | |
18 | ||
f7a99c5b | 19 | path_get(path); |
2a4419b5 | 20 | spin_lock(&fs->lock); |
c28cc364 | 21 | write_seqcount_begin(&fs->seq); |
3e93cd67 AV |
22 | old_root = fs->root; |
23 | fs->root = *path; | |
c28cc364 | 24 | write_seqcount_end(&fs->seq); |
2a4419b5 | 25 | spin_unlock(&fs->lock); |
3e93cd67 | 26 | if (old_root.dentry) |
f7a99c5b | 27 | path_put(&old_root); |
3e93cd67 AV |
28 | } |
29 | ||
30 | /* | |
31 | * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. | |
32 | * It can block. | |
33 | */ | |
dcf787f3 | 34 | void set_fs_pwd(struct fs_struct *fs, const struct path *path) |
3e93cd67 AV |
35 | { |
36 | struct path old_pwd; | |
37 | ||
f7a99c5b | 38 | path_get(path); |
2a4419b5 | 39 | spin_lock(&fs->lock); |
c28cc364 | 40 | write_seqcount_begin(&fs->seq); |
3e93cd67 AV |
41 | old_pwd = fs->pwd; |
42 | fs->pwd = *path; | |
c28cc364 | 43 | write_seqcount_end(&fs->seq); |
2a4419b5 | 44 | spin_unlock(&fs->lock); |
3e93cd67 AV |
45 | |
46 | if (old_pwd.dentry) | |
f7a99c5b | 47 | path_put(&old_pwd); |
3e93cd67 AV |
48 | } |
49 | ||
82234e61 AV |
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 | ||
dcf787f3 | 58 | void chroot_fs_refs(const struct path *old_root, const struct path *new_root) |
3e93cd67 AV |
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) { | |
82234e61 | 69 | int hits = 0; |
2a4419b5 | 70 | spin_lock(&fs->lock); |
c28cc364 | 71 | write_seqcount_begin(&fs->seq); |
82234e61 AV |
72 | hits += replace_path(&fs->root, old_root, new_root); |
73 | hits += replace_path(&fs->pwd, old_root, new_root); | |
74 | write_seqcount_end(&fs->seq); | |
75 | while (hits--) { | |
3e93cd67 | 76 | count++; |
f7a99c5b | 77 | path_get(new_root); |
3e93cd67 | 78 | } |
2a4419b5 | 79 | spin_unlock(&fs->lock); |
3e93cd67 AV |
80 | } |
81 | task_unlock(p); | |
82 | } while_each_thread(g, p); | |
83 | read_unlock(&tasklist_lock); | |
84 | while (count--) | |
f7a99c5b | 85 | path_put(old_root); |
3e93cd67 AV |
86 | } |
87 | ||
498052bb | 88 | void free_fs_struct(struct fs_struct *fs) |
3e93cd67 | 89 | { |
f7a99c5b AV |
90 | path_put(&fs->root); |
91 | path_put(&fs->pwd); | |
498052bb | 92 | kmem_cache_free(fs_cachep, fs); |
3e93cd67 AV |
93 | } |
94 | ||
95 | void exit_fs(struct task_struct *tsk) | |
96 | { | |
498052bb | 97 | struct fs_struct *fs = tsk->fs; |
3e93cd67 AV |
98 | |
99 | if (fs) { | |
498052bb | 100 | int kill; |
3e93cd67 | 101 | task_lock(tsk); |
2a4419b5 | 102 | spin_lock(&fs->lock); |
3e93cd67 | 103 | tsk->fs = NULL; |
498052bb | 104 | kill = !--fs->users; |
2a4419b5 | 105 | spin_unlock(&fs->lock); |
3e93cd67 | 106 | task_unlock(tsk); |
498052bb AV |
107 | if (kill) |
108 | free_fs_struct(fs); | |
3e93cd67 AV |
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) { | |
498052bb AV |
117 | fs->users = 1; |
118 | fs->in_exec = 0; | |
2a4419b5 | 119 | spin_lock_init(&fs->lock); |
c28cc364 | 120 | seqcount_init(&fs->seq); |
3e93cd67 | 121 | fs->umask = old->umask; |
b3e19d92 NP |
122 | |
123 | spin_lock(&old->lock); | |
124 | fs->root = old->root; | |
f7a99c5b | 125 | path_get(&fs->root); |
b3e19d92 | 126 | fs->pwd = old->pwd; |
f7a99c5b | 127 | path_get(&fs->pwd); |
b3e19d92 | 128 | spin_unlock(&old->lock); |
3e93cd67 AV |
129 | } |
130 | return fs; | |
131 | } | |
132 | ||
133 | int unshare_fs_struct(void) | |
134 | { | |
498052bb AV |
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) | |
3e93cd67 | 140 | return -ENOMEM; |
498052bb AV |
141 | |
142 | task_lock(current); | |
2a4419b5 | 143 | spin_lock(&fs->lock); |
498052bb AV |
144 | kill = !--fs->users; |
145 | current->fs = new_fs; | |
2a4419b5 | 146 | spin_unlock(&fs->lock); |
498052bb AV |
147 | task_unlock(current); |
148 | ||
149 | if (kill) | |
150 | free_fs_struct(fs); | |
151 | ||
3e93cd67 AV |
152 | return 0; |
153 | } | |
154 | EXPORT_SYMBOL_GPL(unshare_fs_struct); | |
155 | ||
ce3b0f8d AV |
156 | int current_umask(void) |
157 | { | |
158 | return current->fs->umask; | |
159 | } | |
160 | EXPORT_SYMBOL(current_umask); | |
161 | ||
3e93cd67 AV |
162 | /* to be mentioned only in INIT_TASK */ |
163 | struct fs_struct init_fs = { | |
498052bb | 164 | .users = 1, |
2a4419b5 | 165 | .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock), |
1ca7d67c | 166 | .seq = SEQCNT_ZERO(init_fs.seq), |
3e93cd67 AV |
167 | .umask = 0022, |
168 | }; |