Re: [PATCH v2] inode: debugfs_create_dir uses mode permission from parent

From: Kees Cook
Date: Fri Apr 27 2018 - 10:59:00 EST


On Fri, Apr 27, 2018 at 6:49 AM, Greg KH <gregkh@xxxxxxxxxxxxxxxxxxx> wrote:
> I'm going to add Kees and the kernel-hardning list here, as I'd like
> their opinions for the patch below.
>
> Kees, do you have any problems with this patch? I know you worked on
> making debugfs more "secure" from non-root users, this should still keep
> the intial mount permissions all fine, right? Anything I'm not
> considering here?

This appears correct to me. I'd like to see some stronger rationale
for why this is needed, just so I have a "design" to compare the
implementation against. :)

Normally, the top-level directory permissions should block all the
subdirectories too. The only time I think of this being needed is if
someone is explicitly bind-mounting a subdirectory to another location
(e.g. Chrome OS does this for the i915 subdirectory). In that case,
I'd expect them to tweak permissions too. Thomas, what's your
use-case?

-Kees

>
> thanks,
>
> greg k-h
>
> On Fri, Apr 27, 2018 at 02:35:47PM +0200, Thomas Richter wrote:
>> Currently function debugfs_create_dir() creates a new
>> directory in the debugfs (usually mounted /sys/kernel/debug)
>> with permission rwxr-xr-x. This is hard coded.
>>
>> Change this to use the parent directory permission.
>>
>> Output before the patch:
>> root@s8360047 ~]# tree -dp -L 1 /sys/kernel/debug/
>> /sys/kernel/debug/
>> âââ [drwxr-xr-x] bdi
>> âââ [drwxr-xr-x] block
>> âââ [drwxr-xr-x] dasd
>> âââ [drwxr-xr-x] device_component
>> âââ [drwxr-xr-x] extfrag
>> âââ [drwxr-xr-x] hid
>> âââ [drwxr-xr-x] kprobes
>> âââ [drwxr-xr-x] kvm
>> âââ [drwxr-xr-x] memblock
>> âââ [drwxr-xr-x] pm_qos
>> âââ [drwxr-xr-x] qdio
>> âââ [drwxr-xr-x] s390
>> âââ [drwxr-xr-x] s390dbf
>> âââ [drwx------] tracing
>>
>> 14 directories
>> [root@s8360047 linux]#
>>
>> Output after the patch:
>> [root@s8360047 ~]# tree -dp -L 1 /sys/kernel/debug/
>> sys/kernel/debug/
>> âââ [drwx------] bdi
>> âââ [drwx------] block
>> âââ [drwx------] dasd
>> âââ [drwx------] device_component
>> âââ [drwx------] extfrag
>> âââ [drwx------] hid
>> âââ [drwx------] kprobes
>> âââ [drwx------] kvm
>> âââ [drwx------] memblock
>> âââ [drwx------] pm_qos
>> âââ [drwx------] qdio
>> âââ [drwx------] s390
>> âââ [drwx------] s390dbf
>> âââ [drwx------] tracing
>>
>> 14 directories
>> [root@s8360047 linux]#
>>
>> Here is the full diff output done with:
>> [root@s8360047 ~]# diff -u treefull.before treefull.after |
>> sed 's-^- # -' > treefull.diff
>> # --- treefull.before 2018-04-27 13:22:04.532824564 +0200
>> # +++ treefull.after 2018-04-27 13:24:12.106182062 +0200
>> # @@ -1,55 +1,55 @@
>> # /sys/kernel/debug/
>> # -âââ [drwxr-xr-x] bdi
>> # -â âââ [drwxr-xr-x] 1:0
>> # -â âââ [drwxr-xr-x] 1:1
>> # -â âââ [drwxr-xr-x] 1:10
>> # -â âââ [drwxr-xr-x] 1:11
>> # -â âââ [drwxr-xr-x] 1:12
>> # -â âââ [drwxr-xr-x] 1:13
>> # -â âââ [drwxr-xr-x] 1:14
>> # -â âââ [drwxr-xr-x] 1:15
>> # -â âââ [drwxr-xr-x] 1:2
>> # -â âââ [drwxr-xr-x] 1:3
>> # -â âââ [drwxr-xr-x] 1:4
>> # -â âââ [drwxr-xr-x] 1:5
>> # -â âââ [drwxr-xr-x] 1:6
>> # -â âââ [drwxr-xr-x] 1:7
>> # -â âââ [drwxr-xr-x] 1:8
>> # -â âââ [drwxr-xr-x] 1:9
>> # -â âââ [drwxr-xr-x] 94:0
>> # -âââ [drwxr-xr-x] block
>> # -âââ [drwxr-xr-x] dasd
>> # -â âââ [drwxr-xr-x] 0.0.e18a
>> # -â âââ [drwxr-xr-x] dasda
>> # -â âââ [drwxr-xr-x] global
>> # -âââ [drwxr-xr-x] device_component
>> # -âââ [drwxr-xr-x] extfrag
>> # -âââ [drwxr-xr-x] hid
>> # -âââ [drwxr-xr-x] kprobes
>> # -âââ [drwxr-xr-x] kvm
>> # -âââ [drwxr-xr-x] memblock
>> # -âââ [drwxr-xr-x] pm_qos
>> # -âââ [drwxr-xr-x] qdio
>> # -â âââ [drwxr-xr-x] 0.0.f5f2
>> # -âââ [drwxr-xr-x] s390
>> # -â âââ [drwxr-xr-x] stsi
>> # -âââ [drwxr-xr-x] s390dbf
>> # -â âââ [drwxr-xr-x] 0.0.e18a
>> # -â âââ [drwxr-xr-x] cio_crw
>> # -â âââ [drwxr-xr-x] cio_msg
>> # -â âââ [drwxr-xr-x] cio_trace
>> # -â âââ [drwxr-xr-x] dasd
>> # -â âââ [drwxr-xr-x] kvm-trace
>> # -â âââ [drwxr-xr-x] lgr
>> # -â âââ [drwxr-xr-x] qdio_0.0.f5f2
>> # -â âââ [drwxr-xr-x] qdio_error
>> # -â âââ [drwxr-xr-x] qdio_setup
>> # -â âââ [drwxr-xr-x] qeth_card_0.0.f5f0
>> # -â âââ [drwxr-xr-x] qeth_control
>> # -â âââ [drwxr-xr-x] qeth_msg
>> # -â âââ [drwxr-xr-x] qeth_setup
>> # -â âââ [drwxr-xr-x] vmcp
>> # -â âââ [drwxr-xr-x] vmur
>> # +âââ [drwx------] bdi
>> # +â âââ [drwx------] 1:0
>> # +â âââ [drwx------] 1:1
>> # +â âââ [drwx------] 1:10
>> # +â âââ [drwx------] 1:11
>> # +â âââ [drwx------] 1:12
>> # +â âââ [drwx------] 1:13
>> # +â âââ [drwx------] 1:14
>> # +â âââ [drwx------] 1:15
>> # +â âââ [drwx------] 1:2
>> # +â âââ [drwx------] 1:3
>> # +â âââ [drwx------] 1:4
>> # +â âââ [drwx------] 1:5
>> # +â âââ [drwx------] 1:6
>> # +â âââ [drwx------] 1:7
>> # +â âââ [drwx------] 1:8
>> # +â âââ [drwx------] 1:9
>> # +â âââ [drwx------] 94:0
>> # +âââ [drwx------] block
>> # +âââ [drwx------] dasd
>> # +â âââ [drwx------] 0.0.e18a
>> # +â âââ [drwx------] dasda
>> # +â âââ [drwx------] global
>> # +âââ [drwx------] device_component
>> # +âââ [drwx------] extfrag
>> # +âââ [drwx------] hid
>> # +âââ [drwx------] kprobes
>> # +âââ [drwx------] kvm
>> # +âââ [drwx------] memblock
>> # +âââ [drwx------] pm_qos
>> # +âââ [drwx------] qdio
>> # +â âââ [drwx------] 0.0.f5f2
>> # +âââ [drwx------] s390
>> # +â âââ [drwx------] stsi
>> # +âââ [drwx------] s390dbf
>> # +â âââ [drwx------] 0.0.e18a
>> # +â âââ [drwx------] cio_crw
>> # +â âââ [drwx------] cio_msg
>> # +â âââ [drwx------] cio_trace
>> # +â âââ [drwx------] dasd
>> # +â âââ [drwx------] kvm-trace
>> # +â âââ [drwx------] lgr
>> # +â âââ [drwx------] qdio_0.0.f5f2
>> # +â âââ [drwx------] qdio_error
>> # +â âââ [drwx------] qdio_setup
>> # +â âââ [drwx------] qeth_card_0.0.f5f0
>> # +â âââ [drwx------] qeth_control
>> # +â âââ [drwx------] qeth_msg
>> # +â âââ [drwx------] qeth_setup
>> # +â âââ [drwx------] vmcp
>> # +â âââ [drwx------] vmur
>> # âââ [drwx------] tracing
>> # âââ [drwxr-xr-x] events
>> # â âââ [drwxr-xr-x] alarmtimer
>>
>> Fixes: edac65eaf8d5c ("debugfs: take mode-dependent parts of debugfs_get_inode() into callers")
>> Signed-off-by: Thomas Richter <tmricht@xxxxxxxxxxxxx>
>> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
>> ---
>> fs/debugfs/inode.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
>> index 13b0135..a913b12 100644
>> --- a/fs/debugfs/inode.c
>> +++ b/fs/debugfs/inode.c
>> @@ -512,7 +512,9 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
>> if (unlikely(!inode))
>> return failed_creating(dentry);
>>
>> - inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
>> + if (!parent)
>> + parent = debugfs_mount->mnt_root;
>> + inode->i_mode = S_IFDIR | ((d_inode(parent)->i_mode & 0770));
>> inode->i_op = &simple_dir_inode_operations;
>> inode->i_fop = &simple_dir_operations;
>>
>> --
>> 2.9.3



--
Kees Cook
Pixel Security