Skip to content

Commit 0cbee99

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace
Pull user namespace updates from Eric Biederman: "Long ago and far away when user namespaces where young it was realized that allowing fresh mounts of proc and sysfs with only user namespace permissions could violate the basic rule that only root gets to decide if proc or sysfs should be mounted at all. Some hacks were put in place to reduce the worst of the damage could be done, and the common sense rule was adopted that fresh mounts of proc and sysfs should allow no more than bind mounts of proc and sysfs. Unfortunately that rule has not been fully enforced. There are two kinds of gaps in that enforcement. Only filesystems mounted on empty directories of proc and sysfs should be ignored but the test for empty directories was insufficient. So in my tree directories on proc, sysctl and sysfs that will always be empty are created specially. Every other technique is imperfect as an ordinary directory can have entries added even after a readdir returns and shows that the directory is empty. Special creation of directories for mount points makes the code in the kernel a smidge clearer about it's purpose. I asked container developers from the various container projects to help test this and no holes were found in the set of mount points on proc and sysfs that are created specially. This set of changes also starts enforcing the mount flags of fresh mounts of proc and sysfs are consistent with the existing mount of proc and sysfs. I expected this to be the boring part of the work but unfortunately unprivileged userspace winds up mounting fresh copies of proc and sysfs with noexec and nosuid clear when root set those flags on the previous mount of proc and sysfs. So for now only the atime, read-only and nodev attributes which userspace happens to keep consistent are enforced. Dealing with the noexec and nosuid attributes remains for another time. This set of changes also addresses an issue with how open file descriptors from /proc/<pid>/ns/* are displayed. Recently readlink of /proc/<pid>/fd has been triggering a WARN_ON that has not been meaningful since it was added (as all of the code in the kernel was converted) and is not now actively wrong. There is also a short list of issues that have not been fixed yet that I will mention briefly. It is possible to rename a directory from below to above a bind mount. At which point any directory pointers below the renamed directory can be walked up to the root directory of the filesystem. With user namespaces enabled a bind mount of the bind mount can be created allowing the user to pick a directory whose children they can rename to outside of the bind mount. This is challenging to fix and doubly so because all obvious solutions must touch code that is in the performance part of pathname resolution. As mentioned above there is also a question of how to ensure that developers by accident or with purpose do not introduce exectuable files on sysfs and proc and in doing so introduce security regressions in the current userspace that will not be immediately obvious and as such are likely to require breaking userspace in painful ways once they are recognized" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: vfs: Remove incorrect debugging WARN in prepend_path mnt: Update fs_fully_visible to test for permanently empty directories sysfs: Create mountpoints with sysfs_create_mount_point sysfs: Add support for permanently empty directories to serve as mount points. kernfs: Add support for always empty directories. proc: Allow creating permanently empty directories that serve as mount points sysctl: Allow creating permanently empty directories that serve as mountpoints. fs: Add helper functions for permanently empty directories. vfs: Ignore unlocked mounts in fs_fully_visible mnt: Modify fs_fully_visible to deal with locked ro nodev and atime mnt: Refactor the logic for mounting sysfs and proc in a user namespace
2 parents 2fee94b + 93e3bce commit 0cbee99

28 files changed

Lines changed: 340 additions & 101 deletions

File tree

‎arch/s390/hypfs/inode.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,6 @@ static const struct super_operations hypfs_s_ops = {
456456
.show_options = hypfs_show_options,
457457
};
458458

459-
static struct kobject *s390_kobj;
460-
461459
static int __init hypfs_init(void)
462460
{
463461
int rc;
@@ -481,18 +479,16 @@ static int __init hypfs_init(void)
481479
rc = -ENODATA;
482480
goto fail_hypfs_sprp_exit;
483481
}
484-
s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
485-
if (!s390_kobj) {
486-
rc = -ENOMEM;
482+
rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
483+
if (rc)
487484
goto fail_hypfs_diag0c_exit;
488-
}
489485
rc = register_filesystem(&hypfs_type);
490486
if (rc)
491487
goto fail_filesystem;
492488
return 0;
493489

494490
fail_filesystem:
495-
kobject_put(s390_kobj);
491+
sysfs_remove_mount_point(hypervisor_kobj, "s390");
496492
fail_hypfs_diag0c_exit:
497493
hypfs_diag0c_exit();
498494
fail_hypfs_sprp_exit:
@@ -510,7 +506,7 @@ static int __init hypfs_init(void)
510506
static void __exit hypfs_exit(void)
511507
{
512508
unregister_filesystem(&hypfs_type);
513-
kobject_put(s390_kobj);
509+
sysfs_remove_mount_point(hypervisor_kobj, "s390");
514510
hypfs_diag0c_exit();
515511
hypfs_sprp_exit();
516512
hypfs_vm_exit();

‎drivers/firmware/efi/efi.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ static int __init parse_efi_cmdline(char *str)
6666
early_param("efi", parse_efi_cmdline);
6767

6868
struct kobject *efi_kobj;
69-
static struct kobject *efivars_kobj;
7069

7170
/*
7271
* Let's not leave out systab information that snuck into
@@ -218,10 +217,9 @@ static int __init efisubsys_init(void)
218217
goto err_remove_group;
219218

220219
/* and the standard mountpoint for efivarfs */
221-
efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
222-
if (!efivars_kobj) {
220+
error = sysfs_create_mount_point(efi_kobj, "efivars");
221+
if (error) {
223222
pr_err("efivars: Subsystem registration failed.\n");
224-
error = -ENOMEM;
225223
goto err_remove_group;
226224
}
227225

‎fs/configfs/mount.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ void configfs_release_fs(void)
129129
}
130130

131131

132-
static struct kobject *config_kobj;
133-
134132
static int __init configfs_init(void)
135133
{
136134
int err = -ENOMEM;
@@ -141,8 +139,8 @@ static int __init configfs_init(void)
141139
if (!configfs_dir_cachep)
142140
goto out;
143141

144-
config_kobj = kobject_create_and_add("config", kernel_kobj);
145-
if (!config_kobj)
142+
err = sysfs_create_mount_point(kernel_kobj, "config");
143+
if (err)
146144
goto out2;
147145

148146
err = register_filesystem(&configfs_fs_type);
@@ -152,7 +150,7 @@ static int __init configfs_init(void)
152150
return 0;
153151
out3:
154152
pr_err("Unable to register filesystem!\n");
155-
kobject_put(config_kobj);
153+
sysfs_remove_mount_point(kernel_kobj, "config");
156154
out2:
157155
kmem_cache_destroy(configfs_dir_cachep);
158156
configfs_dir_cachep = NULL;
@@ -163,7 +161,7 @@ static int __init configfs_init(void)
163161
static void __exit configfs_exit(void)
164162
{
165163
unregister_filesystem(&configfs_fs_type);
166-
kobject_put(config_kobj);
164+
sysfs_remove_mount_point(kernel_kobj, "config");
167165
kmem_cache_destroy(configfs_dir_cachep);
168166
configfs_dir_cachep = NULL;
169167
}

‎fs/dcache.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,17 +2927,6 @@ static int prepend_path(const struct path *path,
29272927
vfsmnt = &mnt->mnt;
29282928
continue;
29292929
}
2930-
/*
2931-
* Filesystems needing to implement special "root names"
2932-
* should do so with ->d_dname()
2933-
*/
2934-
if (IS_ROOT(dentry) &&
2935-
(dentry->d_name.len != 1 ||
2936-
dentry->d_name.name[0] != '/')) {
2937-
WARN(1, "Root dentry has weird name <%.*s>\n",
2938-
(int) dentry->d_name.len,
2939-
dentry->d_name.name);
2940-
}
29412930
if (!error)
29422931
error = is_mounted(vfsmnt) ? 1 : 2;
29432932
break;

‎fs/debugfs/inode.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,20 +716,17 @@ bool debugfs_initialized(void)
716716
}
717717
EXPORT_SYMBOL_GPL(debugfs_initialized);
718718

719-
720-
static struct kobject *debug_kobj;
721-
722719
static int __init debugfs_init(void)
723720
{
724721
int retval;
725722

726-
debug_kobj = kobject_create_and_add("debug", kernel_kobj);
727-
if (!debug_kobj)
728-
return -EINVAL;
723+
retval = sysfs_create_mount_point(kernel_kobj, "debug");
724+
if (retval)
725+
return retval;
729726

730727
retval = register_filesystem(&debug_fs_type);
731728
if (retval)
732-
kobject_put(debug_kobj);
729+
sysfs_remove_mount_point(kernel_kobj, "debug");
733730
else
734731
debugfs_registered = true;
735732

‎fs/fuse/inode.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,6 @@ static void fuse_fs_cleanup(void)
12941294
}
12951295

12961296
static struct kobject *fuse_kobj;
1297-
static struct kobject *connections_kobj;
12981297

12991298
static int fuse_sysfs_init(void)
13001299
{
@@ -1306,11 +1305,9 @@ static int fuse_sysfs_init(void)
13061305
goto out_err;
13071306
}
13081307

1309-
connections_kobj = kobject_create_and_add("connections", fuse_kobj);
1310-
if (!connections_kobj) {
1311-
err = -ENOMEM;
1308+
err = sysfs_create_mount_point(fuse_kobj, "connections");
1309+
if (err)
13121310
goto out_fuse_unregister;
1313-
}
13141311

13151312
return 0;
13161313

@@ -1322,7 +1319,7 @@ static int fuse_sysfs_init(void)
13221319

13231320
static void fuse_sysfs_cleanup(void)
13241321
{
1325-
kobject_put(connections_kobj);
1322+
sysfs_remove_mount_point(fuse_kobj, "connections");
13261323
kobject_put(fuse_kobj);
13271324
}
13281325

‎fs/kernfs/dir.c‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ int kernfs_add_one(struct kernfs_node *kn)
592592
goto out_unlock;
593593

594594
ret = -ENOENT;
595+
if (parent->flags & KERNFS_EMPTY_DIR)
596+
goto out_unlock;
597+
595598
if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
596599
goto out_unlock;
597600

@@ -783,6 +786,38 @@ struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
783786
return ERR_PTR(rc);
784787
}
785788

789+
/**
790+
* kernfs_create_empty_dir - create an always empty directory
791+
* @parent: parent in which to create a new directory
792+
* @name: name of the new directory
793+
*
794+
* Returns the created node on success, ERR_PTR() value on failure.
795+
*/
796+
struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
797+
const char *name)
798+
{
799+
struct kernfs_node *kn;
800+
int rc;
801+
802+
/* allocate */
803+
kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, KERNFS_DIR);
804+
if (!kn)
805+
return ERR_PTR(-ENOMEM);
806+
807+
kn->flags |= KERNFS_EMPTY_DIR;
808+
kn->dir.root = parent->dir.root;
809+
kn->ns = NULL;
810+
kn->priv = NULL;
811+
812+
/* link in */
813+
rc = kernfs_add_one(kn);
814+
if (!rc)
815+
return kn;
816+
817+
kernfs_put(kn);
818+
return ERR_PTR(rc);
819+
}
820+
786821
static struct dentry *kernfs_iop_lookup(struct inode *dir,
787822
struct dentry *dentry,
788823
unsigned int flags)
@@ -1254,7 +1289,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
12541289
mutex_lock(&kernfs_mutex);
12551290

12561291
error = -ENOENT;
1257-
if (!kernfs_active(kn) || !kernfs_active(new_parent))
1292+
if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1293+
(new_parent->flags & KERNFS_EMPTY_DIR))
12581294
goto out;
12591295

12601296
error = 0;

‎fs/kernfs/inode.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
296296
case KERNFS_DIR:
297297
inode->i_op = &kernfs_dir_iops;
298298
inode->i_fop = &kernfs_dir_fops;
299+
if (kn->flags & KERNFS_EMPTY_DIR)
300+
make_empty_dir_inode(inode);
299301
break;
300302
case KERNFS_FILE:
301303
inode->i_size = kn->attr.size;

‎fs/libfs.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,98 @@ const struct inode_operations simple_symlink_inode_operations = {
11081108
.readlink = generic_readlink
11091109
};
11101110
EXPORT_SYMBOL(simple_symlink_inode_operations);
1111+
1112+
/*
1113+
* Operations for a permanently empty directory.
1114+
*/
1115+
static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1116+
{
1117+
return ERR_PTR(-ENOENT);
1118+
}
1119+
1120+
static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1121+
struct kstat *stat)
1122+
{
1123+
struct inode *inode = d_inode(dentry);
1124+
generic_fillattr(inode, stat);
1125+
return 0;
1126+
}
1127+
1128+
static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1129+
{
1130+
return -EPERM;
1131+
}
1132+
1133+
static int empty_dir_setxattr(struct dentry *dentry, const char *name,
1134+
const void *value, size_t size, int flags)
1135+
{
1136+
return -EOPNOTSUPP;
1137+
}
1138+
1139+
static ssize_t empty_dir_getxattr(struct dentry *dentry, const char *name,
1140+
void *value, size_t size)
1141+
{
1142+
return -EOPNOTSUPP;
1143+
}
1144+
1145+
static int empty_dir_removexattr(struct dentry *dentry, const char *name)
1146+
{
1147+
return -EOPNOTSUPP;
1148+
}
1149+
1150+
static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1151+
{
1152+
return -EOPNOTSUPP;
1153+
}
1154+
1155+
static const struct inode_operations empty_dir_inode_operations = {
1156+
.lookup = empty_dir_lookup,
1157+
.permission = generic_permission,
1158+
.setattr = empty_dir_setattr,
1159+
.getattr = empty_dir_getattr,
1160+
.setxattr = empty_dir_setxattr,
1161+
.getxattr = empty_dir_getxattr,
1162+
.removexattr = empty_dir_removexattr,
1163+
.listxattr = empty_dir_listxattr,
1164+
};
1165+
1166+
static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1167+
{
1168+
/* An empty directory has two entries . and .. at offsets 0 and 1 */
1169+
return generic_file_llseek_size(file, offset, whence, 2, 2);
1170+
}
1171+
1172+
static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1173+
{
1174+
dir_emit_dots(file, ctx);
1175+
return 0;
1176+
}
1177+
1178+
static const struct file_operations empty_dir_operations = {
1179+
.llseek = empty_dir_llseek,
1180+
.read = generic_read_dir,
1181+
.iterate = empty_dir_readdir,
1182+
.fsync = noop_fsync,
1183+
};
1184+
1185+
1186+
void make_empty_dir_inode(struct inode *inode)
1187+
{
1188+
set_nlink(inode, 2);
1189+
inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1190+
inode->i_uid = GLOBAL_ROOT_UID;
1191+
inode->i_gid = GLOBAL_ROOT_GID;
1192+
inode->i_rdev = 0;
1193+
inode->i_size = 2;
1194+
inode->i_blkbits = PAGE_SHIFT;
1195+
inode->i_blocks = 0;
1196+
1197+
inode->i_op = &empty_dir_inode_operations;
1198+
inode->i_fop = &empty_dir_operations;
1199+
}
1200+
1201+
bool is_empty_dir_inode(struct inode *inode)
1202+
{
1203+
return (inode->i_fop == &empty_dir_operations) &&
1204+
(inode->i_op == &empty_dir_inode_operations);
1205+
}

0 commit comments

Comments
 (0)