aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJoe Perches <joe@perches.com>2016-10-11 16:52:19 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-10-11 18:06:31 -0400
commit459cf0ae5d6600592a539bc861c84e14ed1a5bcb (patch)
treef4fa23f9bedb60aa3b0d2fae55bfff46f64d63e4 /scripts
parentca0d8929e75ab1f860f61208d46955f280a1b05e (diff)
checkpatch: improve the octal permissions tests
The function calls with octal permissions commonly span multiple lines. The current test is line oriented and fails to find some matches. Make the test use the $stat variable instead of the $line variable to span multiple lines. Also add a few functions to the known functions with permissions list. Move the SYMBOLIC_PERMS test to a separate section to find all the S_<FOO> permissions in any form not just those that have specific function names. This can now find and fix permissions uses like: .mode = S_<FOO> | S_<BAR>; Link: http://lkml.kernel.org/r/b51bab60530912aae4ac420119d465c5b206f19f.1475030406.git.joe@perches.com Signed-off-by: Joe Perches <joe@perches.com> Tested-by: Ramiro Oliveira <roliveir@synopsys.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.pl60
1 files changed, 44 insertions, 16 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3373c65fef1c..a8368d1c4348 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -524,7 +524,11 @@ our @mode_permission_funcs = (
524 ["module_param_array_named", 5], 524 ["module_param_array_named", 5],
525 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 525 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
526 ["proc_create(?:_data|)", 2], 526 ["proc_create(?:_data|)", 2],
527 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 527 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
528 ["IIO_DEV_ATTR_[A-Z_]+", 1],
529 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
530 ["SENSOR_TEMPLATE(?:_2|)", 3],
531 ["__ATTR", 2],
528); 532);
529 533
530#Create a search pattern for all these functions to speed up a loop below 534#Create a search pattern for all these functions to speed up a loop below
@@ -6092,45 +6096,69 @@ sub process {
6092# Mode permission misuses where it seems decimal should be octal 6096# Mode permission misuses where it seems decimal should be octal
6093# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 6097# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6094 if ($^V && $^V ge 5.10.0 && 6098 if ($^V && $^V ge 5.10.0 &&
6099 defined $stat &&
6095 $line =~ /$mode_perms_search/) { 6100 $line =~ /$mode_perms_search/) {
6096 foreach my $entry (@mode_permission_funcs) { 6101 foreach my $entry (@mode_permission_funcs) {
6097 my $func = $entry->[0]; 6102 my $func = $entry->[0];
6098 my $arg_pos = $entry->[1]; 6103 my $arg_pos = $entry->[1];
6099 6104
6105 my $lc = $stat =~ tr@\n@@;
6106 $lc = $lc + $linenr;
6107 my $stat_real = raw_line($linenr, 0);
6108 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6109 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6110 }
6111
6100 my $skip_args = ""; 6112 my $skip_args = "";
6101 if ($arg_pos > 1) { 6113 if ($arg_pos > 1) {
6102 $arg_pos--; 6114 $arg_pos--;
6103 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 6115 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6104 } 6116 }
6105 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6117 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6106 if ($line =~ /$test/) { 6118 if ($stat =~ /$test/) {
6107 my $val = $1; 6119 my $val = $1;
6108 $val = $6 if ($skip_args ne ""); 6120 $val = $6 if ($skip_args ne "");
6109 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 6121 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6110 ($val =~ /^$Octal$/ && length($val) ne 4)) { 6122 ($val =~ /^$Octal$/ && length($val) ne 4)) {
6111 ERROR("NON_OCTAL_PERMISSIONS", 6123 ERROR("NON_OCTAL_PERMISSIONS",
6112 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 6124 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6113 } 6125 }
6114 if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6126 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6115 ERROR("EXPORTED_WORLD_WRITABLE", 6127 ERROR("EXPORTED_WORLD_WRITABLE",
6116 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 6128 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6117 }
6118 if ($val =~ /\b$mode_perms_string_search\b/) {
6119 my $to = 0;
6120 while ($val =~ /\b($mode_perms_string_search)\b(?:\s*\|\s*)?\s*/g) {
6121 $to |= $mode_permission_string_types{$1};
6122 }
6123 my $new = sprintf("%04o", $to);
6124 if (WARN("SYMBOLIC_PERMS",
6125 "Symbolic permissions are not preferred. Consider using octal permissions $new.\n" . $herecurr) &&
6126 $fix) {
6127 $fixed[$fixlinenr] =~ s/\Q$val\E/$new/;
6128 }
6129 } 6129 }
6130 } 6130 }
6131 } 6131 }
6132 } 6132 }
6133 6133
6134# check for uses of S_<PERMS> that could be octal for readability
6135 if ($line =~ /\b$mode_perms_string_search\b/) {
6136 my $val = "";
6137 my $oval = "";
6138 my $to = 0;
6139 my $curpos = 0;
6140 my $lastpos = 0;
6141 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6142 $curpos = pos($line);
6143 my $match = $2;
6144 my $omatch = $1;
6145 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6146 $lastpos = $curpos;
6147 $to |= $mode_permission_string_types{$match};
6148 $val .= '\s*\|\s*' if ($val ne "");
6149 $val .= $match;
6150 $oval .= $omatch;
6151 }
6152 $oval =~ s/^\s*\|\s*//;
6153 $oval =~ s/\s*\|\s*$//;
6154 my $octal = sprintf("%04o", $to);
6155 if (WARN("SYMBOLIC_PERMS",
6156 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6157 $fix) {
6158 $fixed[$fixlinenr] =~ s/$val/$octal/;
6159 }
6160 }
6161
6134# validate content of MODULE_LICENSE against list from include/linux/module.h 6162# validate content of MODULE_LICENSE against list from include/linux/module.h
6135 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 6163 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6136 my $extracted_string = get_quoted_string($line, $rawline); 6164 my $extracted_string = get_quoted_string($line, $rawline);