Skip to content

Commit 498052b

Browse files
author
Al Viro
committed
New locking/refcounting for fs_struct
* all changes of current->fs are done under task_lock and write_lock of old fs->lock * refcount is not atomic anymore (same protection) * its decrements are done when removing reference from current; at the same time we decide whether to free it. * put_fs_struct() is gone * new field - ->in_exec. Set by check_unsafe_exec() if we are trying to do execve() and only subthreads share fs_struct. Cleared when finishing exec (success and failure alike). Makes CLONE_FS fail with -EAGAIN if set. * check_unsafe_exec() may fail with -EAGAIN if another execve() from subthread is in progress. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 3e93cd6 commit 498052b

7 files changed

Lines changed: 121 additions & 44 deletions

File tree

‎fs/compat.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <linux/poll.h>
5252
#include <linux/mm.h>
5353
#include <linux/eventpoll.h>
54+
#include <linux/fs_struct.h>
5455

5556
#include <asm/uaccess.h>
5657
#include <asm/mmu_context.h>
@@ -1441,12 +1442,15 @@ int compat_do_execve(char * filename,
14411442
bprm->cred = prepare_exec_creds();
14421443
if (!bprm->cred)
14431444
goto out_unlock;
1444-
check_unsafe_exec(bprm);
1445+
1446+
retval = check_unsafe_exec(bprm);
1447+
if (retval)
1448+
goto out_unlock;
14451449

14461450
file = open_exec(filename);
14471451
retval = PTR_ERR(file);
14481452
if (IS_ERR(file))
1449-
goto out_unlock;
1453+
goto out_unmark;
14501454

14511455
sched_exec();
14521456

@@ -1488,6 +1492,9 @@ int compat_do_execve(char * filename,
14881492
goto out;
14891493

14901494
/* execve succeeded */
1495+
write_lock(&current->fs->lock);
1496+
current->fs->in_exec = 0;
1497+
write_unlock(&current->fs->lock);
14911498
current->in_execve = 0;
14921499
mutex_unlock(&current->cred_exec_mutex);
14931500
acct_update_integrals(current);
@@ -1506,6 +1513,11 @@ int compat_do_execve(char * filename,
15061513
fput(bprm->file);
15071514
}
15081515

1516+
out_unmark:
1517+
write_lock(&current->fs->lock);
1518+
current->fs->in_exec = 0;
1519+
write_unlock(&current->fs->lock);
1520+
15091521
out_unlock:
15101522
current->in_execve = 0;
15111523
mutex_unlock(&current->cred_exec_mutex);

‎fs/exec.c‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,28 +1056,38 @@ EXPORT_SYMBOL(install_exec_creds);
10561056
* - the caller must hold current->cred_exec_mutex to protect against
10571057
* PTRACE_ATTACH
10581058
*/
1059-
void check_unsafe_exec(struct linux_binprm *bprm)
1059+
int check_unsafe_exec(struct linux_binprm *bprm)
10601060
{
10611061
struct task_struct *p = current, *t;
10621062
unsigned long flags;
10631063
unsigned n_fs, n_sighand;
1064+
int res = 0;
10641065

10651066
bprm->unsafe = tracehook_unsafe_exec(p);
10661067

10671068
n_fs = 1;
10681069
n_sighand = 1;
1070+
write_lock(&p->fs->lock);
10691071
lock_task_sighand(p, &flags);
10701072
for (t = next_thread(p); t != p; t = next_thread(t)) {
10711073
if (t->fs == p->fs)
10721074
n_fs++;
10731075
n_sighand++;
10741076
}
10751077

1076-
if (atomic_read(&p->fs->count) > n_fs ||
1077-
atomic_read(&p->sighand->count) > n_sighand)
1078+
if (p->fs->users > n_fs ||
1079+
atomic_read(&p->sighand->count) > n_sighand) {
10781080
bprm->unsafe |= LSM_UNSAFE_SHARE;
1081+
} else {
1082+
if (p->fs->in_exec)
1083+
res = -EAGAIN;
1084+
p->fs->in_exec = 1;
1085+
}
10791086

10801087
unlock_task_sighand(p, &flags);
1088+
write_unlock(&p->fs->lock);
1089+
1090+
return res;
10811091
}
10821092

10831093
/*
@@ -1296,12 +1306,15 @@ int do_execve(char * filename,
12961306
bprm->cred = prepare_exec_creds();
12971307
if (!bprm->cred)
12981308
goto out_unlock;
1299-
check_unsafe_exec(bprm);
1309+
1310+
retval = check_unsafe_exec(bprm);
1311+
if (retval)
1312+
goto out_unlock;
13001313

13011314
file = open_exec(filename);
13021315
retval = PTR_ERR(file);
13031316
if (IS_ERR(file))
1304-
goto out_unlock;
1317+
goto out_unmark;
13051318

13061319
sched_exec();
13071320

@@ -1344,6 +1357,9 @@ int do_execve(char * filename,
13441357
goto out;
13451358

13461359
/* execve succeeded */
1360+
write_lock(&current->fs->lock);
1361+
current->fs->in_exec = 0;
1362+
write_unlock(&current->fs->lock);
13471363
current->in_execve = 0;
13481364
mutex_unlock(&current->cred_exec_mutex);
13491365
acct_update_integrals(current);
@@ -1362,6 +1378,11 @@ int do_execve(char * filename,
13621378
fput(bprm->file);
13631379
}
13641380

1381+
out_unmark:
1382+
write_lock(&current->fs->lock);
1383+
current->fs->in_exec = 0;
1384+
write_unlock(&current->fs->lock);
1385+
13651386
out_unlock:
13661387
current->in_execve = 0;
13671388
mutex_unlock(&current->cred_exec_mutex);

‎fs/fs_struct.c‎

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,27 @@ void chroot_fs_refs(struct path *old_root, struct path *new_root)
7272
path_put(old_root);
7373
}
7474

75-
void put_fs_struct(struct fs_struct *fs)
75+
void free_fs_struct(struct fs_struct *fs)
7676
{
77-
/* No need to hold fs->lock if we are killing it */
78-
if (atomic_dec_and_test(&fs->count)) {
79-
path_put(&fs->root);
80-
path_put(&fs->pwd);
81-
kmem_cache_free(fs_cachep, fs);
82-
}
77+
path_put(&fs->root);
78+
path_put(&fs->pwd);
79+
kmem_cache_free(fs_cachep, fs);
8380
}
8481

8582
void exit_fs(struct task_struct *tsk)
8683
{
87-
struct fs_struct * fs = tsk->fs;
84+
struct fs_struct *fs = tsk->fs;
8885

8986
if (fs) {
87+
int kill;
9088
task_lock(tsk);
89+
write_lock(&fs->lock);
9190
tsk->fs = NULL;
91+
kill = !--fs->users;
92+
write_unlock(&fs->lock);
9293
task_unlock(tsk);
93-
put_fs_struct(fs);
94+
if (kill)
95+
free_fs_struct(fs);
9496
}
9597
}
9698

@@ -99,7 +101,8 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
99101
struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
100102
/* We don't need to lock fs - think why ;-) */
101103
if (fs) {
102-
atomic_set(&fs->count, 1);
104+
fs->users = 1;
105+
fs->in_exec = 0;
103106
rwlock_init(&fs->lock);
104107
fs->umask = old->umask;
105108
read_lock(&old->lock);
@@ -114,28 +117,54 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
114117

115118
int unshare_fs_struct(void)
116119
{
117-
struct fs_struct *fsp = copy_fs_struct(current->fs);
118-
if (!fsp)
120+
struct fs_struct *fs = current->fs;
121+
struct fs_struct *new_fs = copy_fs_struct(fs);
122+
int kill;
123+
124+
if (!new_fs)
119125
return -ENOMEM;
120-
exit_fs(current);
121-
current->fs = fsp;
126+
127+
task_lock(current);
128+
write_lock(&fs->lock);
129+
kill = !--fs->users;
130+
current->fs = new_fs;
131+
write_unlock(&fs->lock);
132+
task_unlock(current);
133+
134+
if (kill)
135+
free_fs_struct(fs);
136+
122137
return 0;
123138
}
124139
EXPORT_SYMBOL_GPL(unshare_fs_struct);
125140

126141
/* to be mentioned only in INIT_TASK */
127142
struct fs_struct init_fs = {
128-
.count = ATOMIC_INIT(1),
143+
.users = 1,
129144
.lock = __RW_LOCK_UNLOCKED(init_fs.lock),
130145
.umask = 0022,
131146
};
132147

133148
void daemonize_fs_struct(void)
134149
{
135-
struct fs_struct *fs;
150+
struct fs_struct *fs = current->fs;
151+
152+
if (fs) {
153+
int kill;
154+
155+
task_lock(current);
136156

137-
exit_fs(current); /* current->fs->count--; */
138-
fs = &init_fs;
139-
current->fs = fs;
140-
atomic_inc(&fs->count);
157+
write_lock(&init_fs.lock);
158+
init_fs.users++;
159+
write_unlock(&init_fs.lock);
160+
161+
write_lock(&fs->lock);
162+
current->fs = &init_fs;
163+
kill = !--fs->users;
164+
write_unlock(&fs->lock);
165+
166+
task_unlock(current);
167+
if (kill)
168+
free_fs_struct(fs);
169+
}
141170
}

‎fs/internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern void __init chrdev_init(void);
4444
/*
4545
* exec.c
4646
*/
47-
extern void check_unsafe_exec(struct linux_binprm *);
47+
extern int check_unsafe_exec(struct linux_binprm *);
4848

4949
/*
5050
* namespace.c

‎fs/proc/task_nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
4949
else
5050
bytes += kobjsize(mm);
5151

52-
if (current->fs && atomic_read(&current->fs->count) > 1)
52+
if (current->fs && current->fs->users > 1)
5353
sbytes += kobjsize(current->fs);
5454
else
5555
bytes += kobjsize(current->fs);

‎include/linux/fs_struct.h‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
#include <linux/path.h>
55

66
struct fs_struct {
7-
atomic_t count; /* This usage count is used by check_unsafe_exec() for
8-
* security checking purposes - therefore it may not be
9-
* incremented, except by clone(CLONE_FS).
10-
*/
7+
int users;
118
rwlock_t lock;
129
int umask;
10+
int in_exec;
1311
struct path root, pwd;
1412
};
1513

@@ -19,7 +17,7 @@ extern void exit_fs(struct task_struct *);
1917
extern void set_fs_root(struct fs_struct *, struct path *);
2018
extern void set_fs_pwd(struct fs_struct *, struct path *);
2119
extern struct fs_struct *copy_fs_struct(struct fs_struct *);
22-
extern void put_fs_struct(struct fs_struct *);
20+
extern void free_fs_struct(struct fs_struct *);
2321
extern void daemonize_fs_struct(void);
2422
extern int unshare_fs_struct(void);
2523

‎kernel/fork.c‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,19 @@ static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
683683

684684
static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
685685
{
686+
struct fs_struct *fs = current->fs;
686687
if (clone_flags & CLONE_FS) {
687-
atomic_inc(&current->fs->count);
688+
/* tsk->fs is already what we want */
689+
write_lock(&fs->lock);
690+
if (fs->in_exec) {
691+
write_unlock(&fs->lock);
692+
return -EAGAIN;
693+
}
694+
fs->users++;
695+
write_unlock(&fs->lock);
688696
return 0;
689697
}
690-
tsk->fs = copy_fs_struct(current->fs);
698+
tsk->fs = copy_fs_struct(fs);
691699
if (!tsk->fs)
692700
return -ENOMEM;
693701
return 0;
@@ -1518,12 +1526,16 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
15181526
{
15191527
struct fs_struct *fs = current->fs;
15201528

1521-
if ((unshare_flags & CLONE_FS) &&
1522-
(fs && atomic_read(&fs->count) > 1)) {
1523-
*new_fsp = copy_fs_struct(current->fs);
1524-
if (!*new_fsp)
1525-
return -ENOMEM;
1526-
}
1529+
if (!(unshare_flags & CLONE_FS) || !fs)
1530+
return 0;
1531+
1532+
/* don't need lock here; in the worst case we'll do useless copy */
1533+
if (fs->users == 1)
1534+
return 0;
1535+
1536+
*new_fsp = copy_fs_struct(fs);
1537+
if (!*new_fsp)
1538+
return -ENOMEM;
15271539

15281540
return 0;
15291541
}
@@ -1639,8 +1651,13 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
16391651

16401652
if (new_fs) {
16411653
fs = current->fs;
1654+
write_lock(&fs->lock);
16421655
current->fs = new_fs;
1643-
new_fs = fs;
1656+
if (--fs->users)
1657+
new_fs = NULL;
1658+
else
1659+
new_fs = fs;
1660+
write_unlock(&fs->lock);
16441661
}
16451662

16461663
if (new_mm) {
@@ -1679,7 +1696,7 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
16791696

16801697
bad_unshare_cleanup_fs:
16811698
if (new_fs)
1682-
put_fs_struct(new_fs);
1699+
free_fs_struct(new_fs);
16831700

16841701
bad_unshare_cleanup_thread:
16851702
bad_unshare_out:

0 commit comments

Comments
 (0)