Skip to content

Commit f667aef

Browse files
Qu Wenruomasoncl
authored andcommitted
btrfs: Make btrfs handle security mount options internally to avoid losing security label.
[BUG] Originally when mount btrfs with "-o subvol=" mount option, btrfs will lose all security lable. And if the btrfs fs is mounted somewhere else, due to the lost of security lable, SELinux will refuse to mount since the same super block is being mounted using different security lable. [REPRODUCER] With SELinux enabled: #mkfs -t btrfs /dev/sda5 #mount -o context=system_u:object_r:nfs_t:s0 /dev/sda5 /mnt/btrfs #btrfs subvolume create /mnt/btrfs/subvol #mount -o subvol=subvol,context=system_u:object_r:nfs_t:s0 /dev/sda5 /mnt/test kernel message: SELinux: mount invalid. Same superblock, different security settings for (dev sda5, type btrfs) [REASON] This happens because btrfs will call vfs_kern_mount() and then mount_subtree() to handle subvolume name lookup. First mount will cut off all the security lables and when it comes to the second vfs_kern_mount(), it has no security label now. [FIX] This patch will makes btrfs behavior much more like nfs, which has the type flag FS_BINARY_MOUNTDATA, making btrfs handles the security label internally. So security label will be set in the real mount time and won't lose label when use with "subvol=" mount option. Reported-by: Eryu Guan <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 0ec31a6 commit f667aef

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

fs/btrfs/ctree.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/pagemap.h>
3535
#include <linux/btrfs.h>
3636
#include <linux/workqueue.h>
37+
#include <linux/security.h>
3738
#include "extent_io.h"
3839
#include "extent_map.h"
3940
#include "async-thread.h"
@@ -1725,6 +1726,9 @@ struct btrfs_fs_info {
17251726

17261727
spinlock_t unused_bgs_lock;
17271728
struct list_head unused_bgs;
1729+
1730+
/* For btrfs to record security options */
1731+
struct security_mnt_opts security_opts;
17281732
};
17291733

17301734
struct btrfs_subvolume_writers {
@@ -3591,6 +3595,7 @@ static inline void free_fs_info(struct btrfs_fs_info *fs_info)
35913595
kfree(fs_info->uuid_root);
35923596
kfree(fs_info->super_copy);
35933597
kfree(fs_info->super_for_commit);
3598+
security_free_mnt_opts(&fs_info->security_opts);
35943599
kfree(fs_info);
35953600
}
35963601

fs/btrfs/super.c

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,54 @@ static struct dentry *mount_subvol(const char *subvol_name, int flags,
12151215
return root;
12161216
}
12171217

1218+
static int parse_security_options(char *orig_opts,
1219+
struct security_mnt_opts *sec_opts)
1220+
{
1221+
char *secdata = NULL;
1222+
int ret = 0;
1223+
1224+
secdata = alloc_secdata();
1225+
if (!secdata)
1226+
return -ENOMEM;
1227+
ret = security_sb_copy_data(orig_opts, secdata);
1228+
if (ret) {
1229+
free_secdata(secdata);
1230+
return ret;
1231+
}
1232+
ret = security_sb_parse_opts_str(secdata, sec_opts);
1233+
free_secdata(secdata);
1234+
return ret;
1235+
}
1236+
1237+
static int setup_security_options(struct btrfs_fs_info *fs_info,
1238+
struct super_block *sb,
1239+
struct security_mnt_opts *sec_opts)
1240+
{
1241+
int ret = 0;
1242+
1243+
/*
1244+
* Call security_sb_set_mnt_opts() to check whether new sec_opts
1245+
* is valid.
1246+
*/
1247+
ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1248+
if (ret)
1249+
return ret;
1250+
1251+
if (!fs_info->security_opts.num_mnt_opts) {
1252+
/* first time security setup, copy sec_opts to fs_info */
1253+
memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1254+
} else {
1255+
/*
1256+
* Since SELinux(the only one supports security_mnt_opts) does
1257+
* NOT support changing context during remount/mount same sb,
1258+
* This must be the same or part of the same security options,
1259+
* just free it.
1260+
*/
1261+
security_free_mnt_opts(sec_opts);
1262+
}
1263+
return ret;
1264+
}
1265+
12181266
/*
12191267
* Find a superblock for the given device / mount point.
12201268
*
@@ -1229,6 +1277,7 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
12291277
struct dentry *root;
12301278
struct btrfs_fs_devices *fs_devices = NULL;
12311279
struct btrfs_fs_info *fs_info = NULL;
1280+
struct security_mnt_opts new_sec_opts;
12321281
fmode_t mode = FMODE_READ;
12331282
char *subvol_name = NULL;
12341283
u64 subvol_objectid = 0;
@@ -1251,9 +1300,16 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
12511300
return root;
12521301
}
12531302

1303+
security_init_mnt_opts(&new_sec_opts);
1304+
if (data) {
1305+
error = parse_security_options(data, &new_sec_opts);
1306+
if (error)
1307+
return ERR_PTR(error);
1308+
}
1309+
12541310
error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
12551311
if (error)
1256-
return ERR_PTR(error);
1312+
goto error_sec_opts;
12571313

12581314
/*
12591315
* Setup a dummy root and fs_info for test/set super. This is because
@@ -1262,13 +1318,16 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
12621318
* then open_ctree will properly initialize everything later.
12631319
*/
12641320
fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
1265-
if (!fs_info)
1266-
return ERR_PTR(-ENOMEM);
1321+
if (!fs_info) {
1322+
error = -ENOMEM;
1323+
goto error_sec_opts;
1324+
}
12671325

12681326
fs_info->fs_devices = fs_devices;
12691327

12701328
fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
12711329
fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1330+
security_init_mnt_opts(&fs_info->security_opts);
12721331
if (!fs_info->super_copy || !fs_info->super_for_commit) {
12731332
error = -ENOMEM;
12741333
goto error_fs_info;
@@ -1306,15 +1365,28 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
13061365
}
13071366

13081367
root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
1309-
if (IS_ERR(root))
1368+
if (IS_ERR(root)) {
1369+
deactivate_locked_super(s);
1370+
error = PTR_ERR(root);
1371+
goto error_sec_opts;
1372+
}
1373+
1374+
fs_info = btrfs_sb(s);
1375+
error = setup_security_options(fs_info, s, &new_sec_opts);
1376+
if (error) {
1377+
dput(root);
13101378
deactivate_locked_super(s);
1379+
goto error_sec_opts;
1380+
}
13111381

13121382
return root;
13131383

13141384
error_close_devices:
13151385
btrfs_close_devices(fs_devices);
13161386
error_fs_info:
13171387
free_fs_info(fs_info);
1388+
error_sec_opts:
1389+
security_free_mnt_opts(&new_sec_opts);
13181390
return ERR_PTR(error);
13191391
}
13201392

@@ -1396,6 +1468,21 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
13961468
sync_filesystem(sb);
13971469
btrfs_remount_prepare(fs_info);
13981470

1471+
if (data) {
1472+
struct security_mnt_opts new_sec_opts;
1473+
1474+
security_init_mnt_opts(&new_sec_opts);
1475+
ret = parse_security_options(data, &new_sec_opts);
1476+
if (ret)
1477+
goto restore;
1478+
ret = setup_security_options(fs_info, sb,
1479+
&new_sec_opts);
1480+
if (ret) {
1481+
security_free_mnt_opts(&new_sec_opts);
1482+
goto restore;
1483+
}
1484+
}
1485+
13991486
ret = btrfs_parse_options(root, data);
14001487
if (ret) {
14011488
ret = -EINVAL;
@@ -1775,7 +1862,7 @@ static struct file_system_type btrfs_fs_type = {
17751862
.name = "btrfs",
17761863
.mount = btrfs_mount,
17771864
.kill_sb = btrfs_kill_super,
1778-
.fs_flags = FS_REQUIRES_DEV,
1865+
.fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
17791866
};
17801867
MODULE_ALIAS_FS("btrfs");
17811868

0 commit comments

Comments
 (0)