Skip to contentSkip to conversation

checkpatch: catch all world writable debugfs_create_file

PATCH In review Nicholas Mc Guire 5 participants 9 messages Mar 13, 2015 – Mar 18, 2015 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: catch all world writable debugfs_create_file

Currently checkpatch will fuss if one uses world writable settings in debugfs files by passing S_IWUGO but not when passing S_IWOTH, S_IRWXUGO or S_IALLUGO. This patch extends the check to catches all cases exporting world writable files

---

The patch was tested against a set of trivial test-cases:
t_file = debugfs_create_file("id", S_IWOTH, t_dir, NULL, &t_fops); t_file = debugfs_create_file("id", S_IWUGO, t_dir, NULL, &t_fops); t_file = debugfs_create_file("id", S_IRWXUGO, t_dir, NULL, &t_fops); t_file = debugfs_create_file("id", S_IALLUGO, t_dir, NULL, &t_fops); and
t_file = debugfs_create_file("id", S_IWOTH | S_IXOTH, t_dir, NULL, &t_fops);

Patch is against 4.0-rc3 (localversion-next is -next-20150313)

 scripts/checkpatch.pl |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +8 -2
index 6b79beb..5def21c 100755
@@ -5356,8 +5356,14 @@ sub process {
 			}
 		}
 
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_file.*S_IWOTH/ ||
+		    $line =~ /debugfs_create_file.*S_IWUGO/ ||
+		    $line =~ /debugfs_create_file.*S_IRWXUGO/ ||
+		    $line =~ /debugfs_create_file.*S_IALLUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IWOTH/ ||
+		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IRWXUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IALLUGO/ ) {
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
 		}
Signature · 1 line
1.7.10.4
maintainer
#1 lore raw
Quoted 4 lines · Nicholas Mc Guire

Currently checkpatch will fuss if one uses world writable settings in debugfs files by passing S_IWUGO but not when passing S_IWOTH, S_IRWXUGO or S_IALLUGO. This patch extends the check to catches all cases exporting world writable files

Hi Nicholas

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -5356,8 +5356,14 @@ sub process {
 			}
 		}
 

scripts/checkpatch.pl sub process { · line 5366

Quoted 10 lines
scripts/checkpatch.pl +8 -2
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_file.*S_IWOTH/ ||
+		    $line =~ /debugfs_create_file.*S_IWUGO/ ||
+		    $line =~ /debugfs_create_file.*S_IRWXUGO/ ||
+		    $line =~ /debugfs_create_file.*S_IALLUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IWOTH/ ||
+		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IRWXUGO/ ||
+		    $line =~ /DEVICE_ATTR.*S_IALLUGO/ ) {

That seems sensible, but my preference would be to use a variable and extend it to find octal values like:

$world_writable = qr{S_IWUGO|S_IWOTH|S_IWUGO|S_URWXUGO|S_IALLUGO|0[0-7][0-7][2367]};

		if ($line =~ /debugfs_create_file.*\b$world_writable\b/
		    $line =~ /DEVICE_ATTR.*\b$world_writable\b/)
Quoted 3 lines
scripts/checkpatch.pl +0 -0
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
 		}
maintainer
#2 lore raw
In reply to Nicholas Mc Guire
Quoted 4 lines · Nicholas Mc Guire

Currently checkpatch will fuss if one uses world writable settings in debugfs files by passing S_IWUGO but not when passing S_IWOTH, S_IRWXUGO or S_IALLUGO. This patch extends the check to catches all cases exporting world writable files

Maybe the debugfs_create_file should be debugfs_create_\w+

And there are also these octal matches:

$ git grep -E -n "debugfs.*\b0[0-7][0-7][2367]"
arch/mips/cavium-octeon/oct_ilm.c:75:   show_dentry = debugfs_create_file("statistics", 0222, dir, NULL,
arch/mips/cavium-octeon/oct_ilm.c:82:   show_dentry = debugfs_create_file("reset", 0222, dir, NULL,
arch/powerpc/kvm/timing.c:226:  debugfs_file = debugfs_create_file(dbg_fname, 0666,
drivers/misc/genwqe/card_debugfs.c:376: file = debugfs_create_x64("err_inject", 0666, root, &cd->err_inject);
drivers/misc/genwqe/card_debugfs.c:382: file = debugfs_create_u32("ddcb_software_timeout", 0666, root,
drivers/misc/genwqe/card_debugfs.c:389: file = debugfs_create_u32("kill_timeout", 0666, root,
drivers/misc/genwqe/card_debugfs.c:461:         file = debugfs_create_u32(name, 0666, root,
drivers/misc/genwqe/card_debugfs.c:483: file = debugfs_create_u32("skip_recovery", 0666, root,
drivers/misc/genwqe/card_debugfs.c:490: file = debugfs_create_u32("use_platform_recovery", 0666, root,
drivers/net/wireless/mac80211_hwsim.c:2452:     debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
drivers/net/wireless/mac80211_hwsim.c:2453:     debugfs_create_file("group", 0666, data->debugfs, data,
drivers/net/wireless/mac80211_hwsim.c:2456:             debugfs_create_file("dfs_simulate_radar", 0222,
drivers/power/axp288_fuel_gauge.c:333:  info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL,
drivers/power/da9030_battery.c:191:     charger->debug_file = debugfs_create_file("charger", 0666, NULL,
kernel/trace/blktrace.c:499:    bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
maintainer
#3 lore raw

PATCH 1/1 checkpatch: match more world writable permissions

In reply to Nicholas Mc Guire

Currently checkpatch will fuss if one uses world writable settings in debugfs files and DEVICE_ATTR uses by testing S_IWUGO but not testing S_IWOTH, S_IRWXUGO or S_IALLUGO.

Extend the check to catch all cases exporting world writable permissions including octal values.

---
 scripts/checkpatch.pl | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +10 -2
index 6b79beb..4f07d50 100755
@@ -443,6 +443,14 @@ foreach my $entry (@mode_permission_funcs) {
 	$mode_perms_search .= $entry->[0];
 }
 
+$our $mode_perms_world_writable = qr{
+	S_IWUGO		|
+	S_IWOTH		|
+	S_IRWXUGO	|
+	S_IALLUGO	|
+	0[0-7][0-7][2367]
+}x;
+
 our $allowed_asm_includes = qr{(?x:
 	irq|
 	memory|
@@ -5356,8 +5364,8 @@ sub process {
 			}
 		}
 
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
+		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
 		}
#4 lore raw

Re: [PATCH] checkpatch: match more world writable permissions

Quoted 35 lines · Joe Perches

Currently checkpatch will fuss if one uses world writable settings in debugfs files and DEVICE_ATTR uses by testing S_IWUGO but not testing S_IWOTH, S_IRWXUGO or S_IALLUGO.

Extend the check to catch all cases exporting world writable permissions including octal values.

---
 scripts/checkpatch.pl | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +8 -0
index 6b79beb..4f07d50 100755
@@ -443,6 +443,14 @@ foreach my $entry (@mode_permission_funcs) {
 	$mode_perms_search .= $entry->[0];
 }
 
+$our $mode_perms_world_writable = qr{
+	S_IWUGO		|
+	S_IWOTH		|
+	S_IRWXUGO	|
+	S_IALLUGO	|
+	0[0-7][0-7][2367]
+}x;
+
 our $allowed_asm_includes = qr{(?x:
 	irq|
 	memory|

scripts/checkpatch.pl sub process { · line 5371

Quoted 10 lines
scripts/checkpatch.pl +2 -2
@@ -5356,8 +5364,8 @@ sub process {
 			}
 		}
 
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
+		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
 		}

yup - thats definitely the clearner solution!

thx!
hofrat

#5 lore raw

Re: [PATCH] checkpatch: match more world writable permissions

In reply to Joe Perches
Quoted 33 lines · Joe Perches

Currently checkpatch will fuss if one uses world writable settings in debugfs files and DEVICE_ATTR uses by testing S_IWUGO but not testing S_IWOTH, S_IRWXUGO or S_IALLUGO.

Extend the check to catch all cases exporting world writable permissions including octal values.

---
 scripts/checkpatch.pl | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +8 -0
index 6b79beb..4f07d50 100755
@@ -443,6 +443,14 @@ foreach my $entry (@mode_permission_funcs) {
 	$mode_perms_search .= $entry->[0];
 }
 
+$our $mode_perms_world_writable = qr{
+	S_IWUGO		|
+	S_IWOTH		|
+	S_IRWXUGO	|
+	S_IALLUGO	|
+	0[0-7][0-7][2367]
+}x;
+
 our $allowed_asm_includes = qr{(?x:
 	irq|
 	memory|

scripts/checkpatch.pl sub process { · line 5370

Quoted 9 lines
scripts/checkpatch.pl +2 -2
@@ -5356,8 +5364,8 @@ sub process {
 			}
 		}
 
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
+		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);

With https://lkml.org/lkml/2015/3/12/412 in mind, maybe this should be marked as error, at least for sysfs attributes.

Thanks,
Guenter

maintainer
#6 lore raw

Re: [PATCH] checkpatch: match more world writable permissions

Quoted 37 lines · Guenter Roeck
Quoted 36 lines · Joe Perches

Currently checkpatch will fuss if one uses world writable settings in debugfs files and DEVICE_ATTR uses by testing S_IWUGO but not testing S_IWOTH, S_IRWXUGO or S_IALLUGO.

Extend the check to catch all cases exporting world writable permissions including octal values.

---
 scripts/checkpatch.pl | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +8 -0
index 6b79beb..4f07d50 100755
@@ -443,6 +443,14 @@ foreach my $entry (@mode_permission_funcs) {
 	$mode_perms_search .= $entry->[0];
 }
 
+$our $mode_perms_world_writable = qr{
+	S_IWUGO		|
+	S_IWOTH		|
+	S_IRWXUGO	|
+	S_IALLUGO	|
+	0[0-7][0-7][2367]
+}x;
+
 our $allowed_asm_includes = qr{(?x:
 	irq|
 	memory|
@@ -5356,8 +5364,8 @@ sub process {
 			}
 		}
 
Quoted 9 lines

scripts/checkpatch.pl sub process { · line 5370

Quoted 6 lines
scripts/checkpatch.pl +2 -2
-		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
-		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
+		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
+		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
 			WARN("EXPORTED_WORLD_WRITABLE",
 			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);

With https://lkml.org/lkml/2015/3/12/412 in mind, maybe this should be marked as error, at least for sysfs attributes.

Maybe it's time for the debate this commit referenced:

commit 58f86cc89c3372d3e61d5b71e5513ec5a0b02848

Author: Rusty Russell <[email protected]>
Date:   Mon Mar 24 12:00:34 2014 +1030

VERIFY_OCTAL_PERMISSIONS: stricter checking for sysfs perms.

Summary of http://lkml.org/lkml/2014/3/14/363 :

Ted: module_param(queue_depth, int, 444)
Joe: 0444!
Rusty: User perms >= group perms >= other perms?
Joe: CLASS_ATTR, DEVICE_ATTR, SENSOR_ATTR and SENSOR_ATTR_2?

Side effect of stricter permissions means removing the unnecessary S_IFREG from several callers.

Note that the BUILD_BUG_ON_ZERO((perm) & 2) test was removed: a fair number of drivers fail this test, so that will be the debate for a future patch.

#7 lore raw

Re: [PATCH] checkpatch: match more world writable permissions

In reply to Joe Perches
Quoted 9 lines · On Fri, 13 Mar 2015 16:43:43 -0700 Joe Perches <[email protected]> wrote:

Currently checkpatch will fuss if one uses world writable settings in debugfs files and DEVICE_ATTR uses by testing S_IWUGO but not testing S_IWOTH, S_IRWXUGO or S_IALLUGO.

Extend the check to catch all cases exporting world writable permissions including octal values.

(quoted diff) +0 -0
@@ -443,6 +443,14 @@ foreach my $entry (@mode_permission_funcs) {
 	$mode_perms_search .= $entry->[0];

Review foreach my $entry (@mode_permission_funcs) { · line 453

Quoted 10 lines
(quoted diff) +8 -0
 }
 
+$our $mode_perms_world_writable = qr{
+	S_IWUGO		|
+	S_IWOTH		|
+	S_IRWXUGO	|
+	S_IALLUGO	|
+	0[0-7][0-7][2367]
+}x;
+

Scalar found where operator expected at scripts/checkpatch.pl line 446, near "$our $mode_perms_world_writable"
(Missing operator before $mode_perms_world_writable?)
Global symbol "$our" requires explicit package name at scripts/checkpatch.pl line 446. syntax error at scripts/checkpatch.pl line 446, near "$our $mode_perms_world_writable " Global symbol "$mode_perms_world_writable" requires explicit package name at scripts/checkpatch.pl line 446. BEGIN not safe after errors--compilation aborted at scripts/checkpatch.pl line 663.

akpm3:/usr/src/25> perl --version

This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi

This?

scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-match-more-world-writable-permissions-fix +1 -1
@@ -443,7 +443,7 @@ foreach my $entry (@mode_permission_func
 	$mode_perms_search .= $entry->[0];
 }
 
-$our $mode_perms_world_writable = qr{
+our $mode_perms_world_writable = qr{
 	S_IWUGO		|
 	S_IWOTH		|
 	S_IRWXUGO	|

_

maintainer
#8 lore raw

Re: [PATCH] checkpatch: match more world writable permissions

Quoted 1 line · Andrew Morton

--- a/scripts/checkpatch.pl~checkpatch-match-more-world-writable-permissions-fix

[]

Quoted 2 lines

-$our $mode_perms_world_writable = qr{
+our $mode_perms_world_writable = qr{

You're a perl monk too? Cool!

Thanks.