linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Andy Whitcroft <apw@canonical.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] checkpatch: Look for symbolic permissions and suggest octal instead
Date: Tue,  2 Aug 2016 16:39:24 -0700	[thread overview]
Message-ID: <7232ef011d05a92f4caa86a5e9830d87966a2eaf.1470180926.git.joe@perches.com> (raw)
In-Reply-To: <CA+55aFw5v23T-zvDZp-MmD_EYxF8WbafwwB59934FV7g21uMGQ@mail.gmail.com>

S_<FOO> uses should be avoided where octal is more intelligible.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 49 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 6 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 1d5b09d..1140940 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -541,6 +541,32 @@ our $mode_perms_world_writable = qr{
 	0[0-7][0-7][2367]
 }x;
 
+our %mode_permission_string_types = (
+	"S_IRWXU" => 0700,
+	"S_IRUSR" => 0400,
+	"S_IWUSR" => 0200,
+	"S_IXUSR" => 0100,
+	"S_IRWXG" => 0070,
+	"S_IRGRP" => 0040,
+	"S_IWGRP" => 0020,
+	"S_IXGRP" => 0010,
+	"S_IRWXO" => 0007,
+	"S_IROTH" => 0004,
+	"S_IWOTH" => 0002,
+	"S_IXOTH" => 0001,
+	"S_IRWXUGO" => 0777,
+	"S_IRUGO" => 0444,
+	"S_IWUGO" => 0222,
+	"S_IXUGO" => 0111,
+);
+
+#Create a search pattern for all these strings to speed up a loop below
+our $mode_perms_string_search = "";
+foreach my $entry (keys %mode_permission_string_types) {
+	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
+	$mode_perms_string_search .= $entry;
+}
+
 our $allowed_asm_includes = qr{(?x:
 	irq|
 	memory|
@@ -5996,20 +6022,31 @@ sub process {
 					$arg_pos--;
 					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
 				}
-				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
+				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
 				if ($line =~ /$test/) {
 					my $val = $1;
 					$val = $6 if ($skip_args ne "");
-
-					if ($val !~ /^0$/ &&
-					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
-					     length($val) ne 4)) {
+					if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
+					    ($val =~ /^$Octal$/ && length($val) ne 4)) {
 						ERROR("NON_OCTAL_PERMISSIONS",
 						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
-					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
+					}
+					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
 						ERROR("EXPORTED_WORLD_WRITABLE",
 						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
 					}
+					if ($val =~ /\b$mode_perms_string_search\b/) {
+						my $to = 0;
+						while ($val =~ /\b($mode_perms_string_search)\b(?:\s*\|\s*)?\s*/g) {
+							$to |=  $mode_permission_string_types{$1};
+						}
+						my $new = sprintf("%04o", $to);
+						if (WARN("SYMBOLIC_PERMS",
+							 "Symbolic permissions are not preferred. Consider using octal permissions $new.\n" . $herecurr) &&
+						    $fix) {
+							$fixed[$fixlinenr] =~ s/\Q$val\E/$new/;
+						}
+					}
 				}
 			}
 		}
-- 
2.8.0.rc4.16.g56331f8

  parent reply	other threads:[~2016-08-02 23:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-02 20:58 Please don't replace numeric parameter like 0444 with macro Linus Torvalds
2016-08-02 21:53 ` Rob Landley
2016-08-02 23:39 ` Joe Perches [this message]
2016-08-03  0:15   ` [PATCH] checkpatch: Look for symbolic permissions and suggest octal instead Al Viro
2016-08-03  0:30     ` Joe Perches
2016-08-15 16:38   ` Joe Perches
2016-08-03  0:42 ` Please don't replace numeric parameter like 0444 with macro Al Viro
2016-08-03  8:07   ` Konstantin Khlebnikov
2016-08-03  8:30     ` Richard Weinberger
2016-08-03  8:11 ` [PATCH] Add file permission mode helpers Ingo Molnar
2016-08-03  8:28   ` Greg Kroah-Hartman
2016-08-03  8:39     ` Ingo Molnar
2016-08-03  9:21       ` Willy Tarreau
2016-08-03  9:53     ` Marcel Holtmann
2016-08-03 15:49   ` Joe Perches
2016-08-03 16:38   ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7232ef011d05a92f4caa86a5e9830d87966a2eaf.1470180926.git.joe@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).