blob: be0250788b737c7633ae155b608502daf3b7c4da [file] [log] [blame]
Paul Gortmaker630d9c42011-11-16 23:57:37 -05001#include <linux/export.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +01002#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +01003#include <linux/sched/task.h>
Al Viro3e93cd62009-03-29 19:00:13 -04004#include <linux/fs.h>
5#include <linux/path.h>
6#include <linux/slab.h>
Al Viro5ad4e532009-03-29 19:50:06 -04007#include <linux/fs_struct.h>
Al Virof03c6592011-01-14 22:30:21 -05008#include "internal.h"
9
Al Viro3e93cd62009-03-29 19:00:13 -040010/*
11 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
12 * It can block.
13 */
Al Virodcf787f2013-03-01 23:51:07 -050014void set_fs_root(struct fs_struct *fs, const struct path *path)
Al Viro3e93cd62009-03-29 19:00:13 -040015{
16 struct path old_root;
17
Al Virof7a99c52012-06-09 00:59:08 -040018 path_get(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100019 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110020 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040021 old_root = fs->root;
22 fs->root = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110023 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100024 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040025 if (old_root.dentry)
Al Virof7a99c52012-06-09 00:59:08 -040026 path_put(&old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040027}
28
29/*
30 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
31 * It can block.
32 */
Al Virodcf787f2013-03-01 23:51:07 -050033void set_fs_pwd(struct fs_struct *fs, const struct path *path)
Al Viro3e93cd62009-03-29 19:00:13 -040034{
35 struct path old_pwd;
36
Al Virof7a99c52012-06-09 00:59:08 -040037 path_get(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100038 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110039 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040040 old_pwd = fs->pwd;
41 fs->pwd = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110042 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100043 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040044
45 if (old_pwd.dentry)
Al Virof7a99c52012-06-09 00:59:08 -040046 path_put(&old_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040047}
48
Al Viro82234e62012-03-15 14:48:55 -040049static 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 Virodcf787f2013-03-01 23:51:07 -050057void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
Al Viro3e93cd62009-03-29 19:00:13 -040058{
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 Viro82234e62012-03-15 14:48:55 -040068 int hits = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +100069 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110070 write_seqcount_begin(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040071 hits += replace_path(&fs->root, old_root, new_root);
72 hits += replace_path(&fs->pwd, old_root, new_root);
Nick Pigginc28cc362011-01-07 17:49:53 +110073 write_seqcount_end(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040074 while (hits--) {
75 count++;
Al Virof7a99c52012-06-09 00:59:08 -040076 path_get(new_root);
Al Viro82234e62012-03-15 14:48:55 -040077 }
Nick Piggin2a4419b2010-08-18 04:37:33 +100078 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040079 }
80 task_unlock(p);
81 } while_each_thread(g, p);
82 read_unlock(&tasklist_lock);
83 while (count--)
Al Virof7a99c52012-06-09 00:59:08 -040084 path_put(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040085}
86
Al Viro498052b2009-03-30 07:20:30 -040087void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040088{
Al Virof7a99c52012-06-09 00:59:08 -040089 path_put(&fs->root);
90 path_put(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -040091 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040092}
93
94void exit_fs(struct task_struct *tsk)
95{
Al Viro498052b2009-03-30 07:20:30 -040096 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040097
98 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -040099 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -0400100 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000101 spin_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400102 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -0400103 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000104 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400105 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400106 if (kill)
107 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400108 }
109}
110
111struct 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 Viro498052b2009-03-30 07:20:30 -0400116 fs->users = 1;
117 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000118 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100119 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400120 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100121
122 spin_lock(&old->lock);
123 fs->root = old->root;
Al Virof7a99c52012-06-09 00:59:08 -0400124 path_get(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100125 fs->pwd = old->pwd;
Al Virof7a99c52012-06-09 00:59:08 -0400126 path_get(&fs->pwd);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100127 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400128 }
129 return fs;
130}
131
132int unshare_fs_struct(void)
133{
Al Viro498052b2009-03-30 07:20:30 -0400134 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 Viro3e93cd62009-03-29 19:00:13 -0400139 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400140
141 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000142 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400143 kill = !--fs->users;
144 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000145 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400146 task_unlock(current);
147
148 if (kill)
149 free_fs_struct(fs);
150
Al Viro3e93cd62009-03-29 19:00:13 -0400151 return 0;
152}
153EXPORT_SYMBOL_GPL(unshare_fs_struct);
154
Al Viroce3b0f82009-03-29 19:08:22 -0400155int current_umask(void)
156{
157 return current->fs->umask;
158}
159EXPORT_SYMBOL(current_umask);
160
Al Viro3e93cd62009-03-29 19:00:13 -0400161/* to be mentioned only in INIT_TASK */
162struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400163 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000164 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
John Stultz1ca7d672013-10-07 15:51:59 -0700165 .seq = SEQCNT_ZERO(init_fs.seq),
Al Viro3e93cd62009-03-29 19:00:13 -0400166 .umask = 0022,
167};