nfs: fix regression in handling of context= option in NFSv4
Subsystems (2)
PATCH 1/1 nfs: fix regression in handling of context= option in NFSv4
Setting the security context of a NFSv4 mount via the context= mount option is currently broken. The NFSv4 codepath allocates a parsed options struct, and then parses the mount options to fill it. It eventually calls nfs4_remote_mount which calls security_init_mnt_opts. That clobbers the lsm_opts struct that was populated earlier. This bug also looks like it causes a small memory leak on each v4 mount where context= is used.
Fix this by moving the initialization of the lsm_opts into nfs_alloc_parsed_mount_data, and the freeing of the same into the functions that allocate the nfs_parsed_mount_data.
I believe this regression was introduced quite some time ago, probably by commit c02d7adf.
--- fs/nfs/super.c | 11 +++-------- 1 files changed, 3 insertions(+), 8 deletions(-)
fs/nfs/super.c +3 -8
@@ -909,6 +909,7 @@ static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int ve data->auth_flavor_len =1 ; data->version = version; data->minorversion =0 ; +security_init_mnt_opts (&data->lsm_opts); }return data; }
@@ -2222,8 +2223,6 @@ static struct dentry *nfs_fs_mount(struct file_system_type *fs_type,if (data ==NULL || mntfh ==NULL ) goto out_free_fh; -security_init_mnt_opts (&data->lsm_opts); -/* Validate the mount data */ error =nfs_validate_mount_data (raw_data, data, mntfh, dev_name);if (error <0 ) {
@@ -2623,9 +2622,7 @@ nfs4_remote_mount(struct file_system_type *fs_type, int flags, mntfh =nfs_alloc_fhandle ();if (data ==NULL || mntfh ==NULL ) - goto out_free_fh; - -security_init_mnt_opts (&data->lsm_opts); + goto out;/* Get a volume representation */ server =nfs4_create_server (data, mntfh);
@@ -2677,13 +2674,10 @@ nfs4_remote_mount(struct file_system_type *fs_type, int flags, s->s_flags |=MS_ACTIVE ; -security_free_mnt_opts (&data->lsm_opts);nfs_free_fhandle (mntfh);return mntroot; out: -security_free_mnt_opts (&data->lsm_opts); -out_free_fh:nfs_free_fhandle (mntfh);return ERR_PTR (error);
@@ -2856,6 +2850,7 @@ out:kfree (data->nfs_server.export_path);kfree (data->nfs_server.hostname);kfree (data->fscache_uniq); +security_free_mnt_opts (&data->lsm_opts); out_free_data:kfree (data); dprintk("<-- nfs4_mount() = %d%s\n", error,
Signature · 1 line
1.7.7.3
Quoted 11 lines · Jeff Layton
Setting the security context of a NFSv4 mount via the context= mount option is currently broken. The NFSv4 codepath allocates a parsed options struct, and then parses the mount options to fill it. It eventually calls
nfs4_remote_mountwhich callssecurity_init_mnt_opts. That clobbers thelsm_optsstruct that was populated earlier. This bug also looks like it causes a small memory leak on each v4 mount where context= is used.Fix this by moving the initialization of the
lsm_optsintonfs_alloc_parsed_mount_data, and the freeing of the same into the functions that allocate thenfs_parsed_mount_data.
I think this is a good lifetime, but I don't think we have it quite right.
fs/nfs/super.c static struct dentry *nfs_fs_mount(struct file_system_type *fs_type, · line 2224
Quoted 3 lines
fs/nfs/super.c +0
-0@@ -2222,8 +2223,6 @@ static struct dentry *nfs_fs_mount(struct file_system_type *fs_type,if (data ==NULL || mntfh ==NULL ) goto out_free_fh;
Lets assume we allocated data, but failed on mntfh. We are going to have called security_init_mnt_opts() but never have called the corresponding destructor. True, it'll be fine today with selinux, but I make no promises what the future holds...
I'm pretty sure the v4 code has the same issue. Maybe you should write an explicit nfs_free_parsed_mount_data() function to handle all of the error paths in v3 and v4? Just a suggestion....
Quoted 25 lines · On Mon, 19 Dec 2011 15:13:05 -0500 Eric Paris <[email protected]> wrote:
Quoted 11 lines · Jeff Layton
Setting the security context of a NFSv4 mount via the context= mount option is currently broken. The NFSv4 codepath allocates a parsed options struct, and then parses the mount options to fill it. It eventually calls
nfs4_remote_mountwhich callssecurity_init_mnt_opts. That clobbers thelsm_optsstruct that was populated earlier. This bug also looks like it causes a small memory leak on each v4 mount where context= is used.Fix this by moving the initialization of the
lsm_optsintonfs_alloc_parsed_mount_data, and the freeing of the same into the functions that allocate thenfs_parsed_mount_data.I think this is a good lifetime, but I don't think we have it quite right.
fs/nfs/super.c static struct dentry *nfs_fs_mount(struct file_system_type *fs_type, · line 2224
Quoted 3 lines
fs/nfs/super.c +0
-0@@ -2222,8 +2223,6 @@ static struct dentry *nfs_fs_mount(struct file_system_type *fs_type,if (data ==NULL || mntfh ==NULL ) goto out_free_fh;Lets assume we allocated data, but failed on mntfh. We are going to have called
security_init_mnt_opts()but never have called the corresponding destructor. True, it'll be fine today with selinux, but I make no promises what the future holds...
Quoted 5 lines
I'm pretty sure the v4 code has the same issue. Maybe you should write an explicit
nfs_free_parsed_mount_data()function to handle all of the error paths in v3 and v4? Just a suggestion....
That's a reasonable suggestion. I'll respin this with a destructor for parsed_mount_data structs.
Thanks,
Signature · 1 line
Jeff Layton <[email protected]>