Skip to content

Commit 2435880

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add checks for constant non-octal permissions
umode_t permissions are sometimes mistakenly written with decimal constants. Verify that numeric permissions are using octal. Add a list of the most commonly used functions and macros that have umode_t permissions and the argument position. Add a $Octal type to $Constant. Allow $LvalOrFunc to be a pointer indirection too. Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 91f72e9 commit 2435880

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,12 @@ sub hash_show_words {
289289
our $Binary = qr{(?i)0b[01]+$Int_type?};
290290
our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
291291
our $Int = qr{[0-9]+$Int_type?};
292+
our $Octal = qr{0[0-7]+$Int_type?};
292293
our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
293294
our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
294295
our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
295296
our $Float = qr{$Float_hex|$Float_dec|$Float_int};
296-
our $Constant = qr{$Float|$Binary|$Hex|$Int};
297+
our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
297298
our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
298299
our $Compare = qr{<=|>=|==|!=|<|>};
299300
our $Arithmetic = qr{\+|-|\*|\/|%};
@@ -378,6 +379,15 @@ sub hash_show_words {
378379
qr{fastcall},
379380
);
380381

382+
our @mode_permission_funcs = (
383+
["module_param", 3],
384+
["module_param_(?:array|named|string)", 4],
385+
["module_param_array_named", 5],
386+
["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
387+
["proc_create(?:_data|)", 2],
388+
["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
389+
);
390+
381391
our $allowed_asm_includes = qr{(?x:
382392
irq|
383393
memory
@@ -423,7 +433,7 @@ sub build_types {
423433
# Any use must be runtime checked with $^V
424434

425435
our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
426-
our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
436+
our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
427437
our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
428438

429439
sub deparenthesize {
@@ -4473,6 +4483,28 @@ sub process {
44734483
WARN("EXPORTED_WORLD_WRITABLE",
44744484
"Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
44754485
}
4486+
4487+
foreach my $entry (@mode_permission_funcs) {
4488+
my $func = $entry->[0];
4489+
my $arg_pos = $entry->[1];
4490+
4491+
my $skip_args = "";
4492+
if ($arg_pos > 1) {
4493+
$arg_pos--;
4494+
$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
4495+
}
4496+
my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4497+
if ($^V && $^V ge 5.10.0 &&
4498+
$line =~ /$test/) {
4499+
my $val = $1;
4500+
$val = $6 if ($skip_args ne "");
4501+
4502+
if ($val =~ /^$Int$/ && $val !~ /^$Octal$/) {
4503+
ERROR("NON_OCTAL_PERMISSIONS",
4504+
"Use octal not decimal permissions\n" . $herecurr);
4505+
}
4506+
}
4507+
}
44764508
}
44774509

44784510
# If we have no input at all, then there is nothing to report on

0 commit comments

Comments
 (0)