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