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