Skip to content

Commit 2d8f303

Browse files
author
Al Viro
committed
[PATCH] sanitize __user_walk_fd() et.al.
* do not pass nameidata; struct path is all the callers want. * switch to new helpers: user_path_at(dfd, pathname, flags, &path) user_path(pathname, &path) user_lpath(pathname, &path) user_path_dir(pathname, &path) (fail if not a directory) The last 3 are trivial macro wrappers for the first one. * remove nameidata in callers. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 256984a commit 2d8f303

13 files changed

Lines changed: 235 additions & 238 deletions

File tree

‎arch/alpha/kernel/osf_sys.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ do_osf_statfs(struct dentry * dentry, struct osf_statfs __user *buffer,
253253
}
254254

255255
asmlinkage int
256-
osf_statfs(char __user *path, struct osf_statfs __user *buffer, unsigned long bufsiz)
256+
osf_statfs(char __user *pathname, struct osf_statfs __user *buffer, unsigned long bufsiz)
257257
{
258-
struct nameidata nd;
258+
struct path path;
259259
int retval;
260260

261-
retval = user_path_walk(path, &nd);
261+
retval = user_path(pathname, &path);
262262
if (!retval) {
263-
retval = do_osf_statfs(nd.path.dentry, buffer, bufsiz);
264-
path_put(&nd.path);
263+
retval = do_osf_statfs(path.dentry, buffer, bufsiz);
264+
path_put(&path);
265265
}
266266
return retval;
267267
}

‎arch/parisc/hpux/sys_hpux.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,19 @@ static int vfs_statfs_hpux(struct dentry *dentry, struct hpux_statfs *buf)
210210
}
211211

212212
/* hpux statfs */
213-
asmlinkage long hpux_statfs(const char __user *path,
213+
asmlinkage long hpux_statfs(const char __user *pathname,
214214
struct hpux_statfs __user *buf)
215215
{
216-
struct nameidata nd;
216+
struct path path;
217217
int error;
218218

219-
error = user_path_walk(path, &nd);
219+
error = user_path(pathname, &path);
220220
if (!error) {
221221
struct hpux_statfs tmp;
222-
error = vfs_statfs_hpux(nd.path.dentry, &tmp);
222+
error = vfs_statfs_hpux(path.dentry, &tmp);
223223
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
224224
error = -EFAULT;
225-
path_put(&nd.path);
225+
path_put(&path);
226226
}
227227
return error;
228228
}

‎fs/coda/pioctl.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int coda_ioctl_permission(struct inode *inode, int mask)
4949
static int coda_pioctl(struct inode * inode, struct file * filp,
5050
unsigned int cmd, unsigned long user_data)
5151
{
52-
struct nameidata nd;
52+
struct path path;
5353
int error;
5454
struct PioctlData data;
5555
struct inode *target_inode = NULL;
@@ -64,21 +64,21 @@ static int coda_pioctl(struct inode * inode, struct file * filp,
6464
* Look up the pathname. Note that the pathname is in
6565
* user memory, and namei takes care of this
6666
*/
67-
if ( data.follow ) {
68-
error = user_path_walk(data.path, &nd);
67+
if (data.follow) {
68+
error = user_path(data.path, &path);
6969
} else {
70-
error = user_path_walk_link(data.path, &nd);
70+
error = user_lpath(data.path, &path);
7171
}
7272

7373
if ( error ) {
7474
return error;
7575
} else {
76-
target_inode = nd.path.dentry->d_inode;
76+
target_inode = path.dentry->d_inode;
7777
}
7878

7979
/* return if it is not a Coda inode */
8080
if ( target_inode->i_sb != inode->i_sb ) {
81-
path_put(&nd.path);
81+
path_put(&path);
8282
return -EINVAL;
8383
}
8484

@@ -87,7 +87,7 @@ static int coda_pioctl(struct inode * inode, struct file * filp,
8787

8888
error = venus_pioctl(inode->i_sb, &(cnp->c_fid), cmd, &data);
8989

90-
path_put(&nd.path);
90+
path_put(&path);
9191
return error;
9292
}
9393

‎fs/compat.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,18 @@ static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *
234234
* The following statfs calls are copies of code from fs/open.c and
235235
* should be checked against those from time to time
236236
*/
237-
asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
237+
asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
238238
{
239-
struct nameidata nd;
239+
struct path path;
240240
int error;
241241

242-
error = user_path_walk(path, &nd);
242+
error = user_path(pathname, &path);
243243
if (!error) {
244244
struct kstatfs tmp;
245-
error = vfs_statfs(nd.path.dentry, &tmp);
245+
error = vfs_statfs(path.dentry, &tmp);
246246
if (!error)
247247
error = put_compat_statfs(buf, &tmp);
248-
path_put(&nd.path);
248+
path_put(&path);
249249
}
250250
return error;
251251
}
@@ -299,21 +299,21 @@ static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstat
299299
return 0;
300300
}
301301

302-
asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
302+
asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
303303
{
304-
struct nameidata nd;
304+
struct path path;
305305
int error;
306306

307307
if (sz != sizeof(*buf))
308308
return -EINVAL;
309309

310-
error = user_path_walk(path, &nd);
310+
error = user_path(pathname, &path);
311311
if (!error) {
312312
struct kstatfs tmp;
313-
error = vfs_statfs(nd.path.dentry, &tmp);
313+
error = vfs_statfs(path.dentry, &tmp);
314314
if (!error)
315315
error = put_compat_statfs64(buf, &tmp);
316-
path_put(&nd.path);
316+
path_put(&path);
317317
}
318318
return error;
319319
}

‎fs/inotify_user.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,20 @@ static void inotify_dev_event_dequeue(struct inotify_device *dev)
354354
}
355355

356356
/*
357-
* find_inode - resolve a user-given path to a specific inode and return a nd
357+
* find_inode - resolve a user-given path to a specific inode
358358
*/
359-
static int find_inode(const char __user *dirname, struct nameidata *nd,
359+
static int find_inode(const char __user *dirname, struct path *path,
360360
unsigned flags)
361361
{
362362
int error;
363363

364-
error = __user_walk(dirname, flags, nd);
364+
error = user_path_at(AT_FDCWD, dirname, flags, path);
365365
if (error)
366366
return error;
367367
/* you can only watch an inode if you have read permissions on it */
368-
error = inode_permission(nd->path.dentry->d_inode, MAY_READ);
368+
error = inode_permission(path->dentry->d_inode, MAY_READ);
369369
if (error)
370-
path_put(&nd->path);
370+
path_put(path);
371371
return error;
372372
}
373373

@@ -650,11 +650,11 @@ asmlinkage long sys_inotify_init(void)
650650
return sys_inotify_init1(0);
651651
}
652652

653-
asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
653+
asmlinkage long sys_inotify_add_watch(int fd, const char __user *pathname, u32 mask)
654654
{
655655
struct inode *inode;
656656
struct inotify_device *dev;
657-
struct nameidata nd;
657+
struct path path;
658658
struct file *filp;
659659
int ret, fput_needed;
660660
unsigned flags = 0;
@@ -674,12 +674,12 @@ asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
674674
if (mask & IN_ONLYDIR)
675675
flags |= LOOKUP_DIRECTORY;
676676

677-
ret = find_inode(path, &nd, flags);
677+
ret = find_inode(pathname, &path, flags);
678678
if (unlikely(ret))
679679
goto fput_and_out;
680680

681-
/* inode held in place by reference to nd; dev by fget on fd */
682-
inode = nd.path.dentry->d_inode;
681+
/* inode held in place by reference to path; dev by fget on fd */
682+
inode = path.dentry->d_inode;
683683
dev = filp->private_data;
684684

685685
mutex_lock(&dev->up_mutex);
@@ -688,7 +688,7 @@ asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
688688
ret = create_watch(dev, inode, mask);
689689
mutex_unlock(&dev->up_mutex);
690690

691-
path_put(&nd.path);
691+
path_put(&path);
692692
fput_and_out:
693693
fput_light(filp, fput_needed);
694694
return ret;

‎fs/namei.c‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,24 +1334,24 @@ struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
13341334
return __lookup_hash(&this, base, NULL);
13351335
}
13361336

1337-
int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1338-
struct nameidata *nd)
1337+
int user_path_at(int dfd, const char __user *name, unsigned flags,
1338+
struct path *path)
13391339
{
1340+
struct nameidata nd;
13401341
char *tmp = getname(name);
13411342
int err = PTR_ERR(tmp);
1342-
13431343
if (!IS_ERR(tmp)) {
1344-
err = do_path_lookup(dfd, tmp, flags, nd);
1344+
1345+
BUG_ON(flags & LOOKUP_PARENT);
1346+
1347+
err = do_path_lookup(dfd, tmp, flags, &nd);
13451348
putname(tmp);
1349+
if (!err)
1350+
*path = nd.path;
13461351
}
13471352
return err;
13481353
}
13491354

1350-
int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1351-
{
1352-
return __user_walk_fd(AT_FDCWD, name, flags, nd);
1353-
}
1354-
13551355
/*
13561356
* It's inline, so penalty for filesystems that don't use sticky bit is
13571357
* minimal.
@@ -2446,7 +2446,8 @@ asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
24462446
int flags)
24472447
{
24482448
struct dentry *new_dentry;
2449-
struct nameidata nd, old_nd;
2449+
struct nameidata nd;
2450+
struct path old_path;
24502451
int error;
24512452
char * to;
24522453

@@ -2457,16 +2458,16 @@ asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
24572458
if (IS_ERR(to))
24582459
return PTR_ERR(to);
24592460

2460-
error = __user_walk_fd(olddfd, oldname,
2461-
flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2462-
&old_nd);
2461+
error = user_path_at(olddfd, oldname,
2462+
flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2463+
&old_path);
24632464
if (error)
24642465
goto exit;
24652466
error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
24662467
if (error)
24672468
goto out;
24682469
error = -EXDEV;
2469-
if (old_nd.path.mnt != nd.path.mnt)
2470+
if (old_path.mnt != nd.path.mnt)
24702471
goto out_release;
24712472
new_dentry = lookup_create(&nd, 0);
24722473
error = PTR_ERR(new_dentry);
@@ -2475,7 +2476,7 @@ asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
24752476
error = mnt_want_write(nd.path.mnt);
24762477
if (error)
24772478
goto out_dput;
2478-
error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
2479+
error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
24792480
mnt_drop_write(nd.path.mnt);
24802481
out_dput:
24812482
dput(new_dentry);
@@ -2484,7 +2485,7 @@ asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
24842485
out_release:
24852486
path_put(&nd.path);
24862487
out:
2487-
path_put(&old_nd.path);
2488+
path_put(&old_path);
24882489
exit:
24892490
putname(to);
24902491

@@ -2877,8 +2878,7 @@ const struct inode_operations page_symlink_inode_operations = {
28772878
.put_link = page_put_link,
28782879
};
28792880

2880-
EXPORT_SYMBOL(__user_walk);
2881-
EXPORT_SYMBOL(__user_walk_fd);
2881+
EXPORT_SYMBOL(user_path_at);
28822882
EXPORT_SYMBOL(follow_down);
28832883
EXPORT_SYMBOL(follow_up);
28842884
EXPORT_SYMBOL(get_write_access); /* binfmt_aout */

0 commit comments

Comments
 (0)