blob: 97c2c52e939849ac8af771bc163675e046818dfc [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}
Greg Kroah-Hartman383de0d2019-10-01 09:41:17 +020048EXPORT_SYMBOL_GPL(set_fs_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040049
Al Viro82234e62012-03-15 14:48:55 -040050static 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 Virodcf787f2013-03-01 23:51:07 -050058void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
Al Viro3e93cd62009-03-29 19:00:13 -040059{
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 Viro82234e62012-03-15 14:48:55 -040069 int hits = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +100070 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110071 write_seqcount_begin(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040072 hits += replace_path(&fs->root, old_root, new_root);
73 hits += replace_path(&fs->pwd, old_root, new_root);
Nick Pigginc28cc362011-01-07 17:49:53 +110074 write_seqcount_end(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040075 while (hits--) {
76 count++;
Al Virof7a99c52012-06-09 00:59:08 -040077 path_get(new_root);
Al Viro82234e62012-03-15 14:48:55 -040078 }
Nick Piggin2a4419b2010-08-18 04:37:33 +100079 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040080 }
81 task_unlock(p);
82 } while_each_thread(g, p);
83 read_unlock(&tasklist_lock);
84 while (count--)
Al Virof7a99c52012-06-09 00:59:08 -040085 path_put(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040086}
87
Al Viro498052b2009-03-30 07:20:30 -040088void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040089{
Al Virof7a99c52012-06-09 00:59:08 -040090 path_put(&fs->root);
91 path_put(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -040092 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040093}
Greg Kroah-Hartman383de0d2019-10-01 09:41:17 +020094EXPORT_SYMBOL_GPL(free_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -040095
96void exit_fs(struct task_struct *tsk)
97{
Al Viro498052b2009-03-30 07:20:30 -040098 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040099
100 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400101 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -0400102 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000103 spin_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400104 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -0400105 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000106 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400107 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400108 if (kill)
109 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400110 }
111}
112
113struct fs_struct *copy_fs_struct(struct fs_struct *old)
114{
115 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
116 /* We don't need to lock fs - think why ;-) */
117 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400118 fs->users = 1;
119 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000120 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100121 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400122 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100123
124 spin_lock(&old->lock);
125 fs->root = old->root;
Al Virof7a99c52012-06-09 00:59:08 -0400126 path_get(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100127 fs->pwd = old->pwd;
Al Virof7a99c52012-06-09 00:59:08 -0400128 path_get(&fs->pwd);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100129 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400130 }
131 return fs;
132}
Daniel Rosenberg50f0dd42018-01-30 14:24:02 -0800133EXPORT_SYMBOL_GPL(copy_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -0400134
135int unshare_fs_struct(void)
136{
Al Viro498052b2009-03-30 07:20:30 -0400137 struct fs_struct *fs = current->fs;
138 struct fs_struct *new_fs = copy_fs_struct(fs);
139 int kill;
140
141 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400142 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400143
144 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000145 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400146 kill = !--fs->users;
147 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000148 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400149 task_unlock(current);
150
151 if (kill)
152 free_fs_struct(fs);
153
Al Viro3e93cd62009-03-29 19:00:13 -0400154 return 0;
155}
156EXPORT_SYMBOL_GPL(unshare_fs_struct);
157
Al Viroce3b0f82009-03-29 19:08:22 -0400158int current_umask(void)
159{
160 return current->fs->umask;
161}
162EXPORT_SYMBOL(current_umask);
163
Al Viro3e93cd62009-03-29 19:00:13 -0400164/* to be mentioned only in INIT_TASK */
165struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400166 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000167 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
John Stultz1ca7d672013-10-07 15:51:59 -0700168 .seq = SEQCNT_ZERO(init_fs.seq),
Al Viro3e93cd62009-03-29 19:00:13 -0400169 .umask = 0022,
170};