Skip to contentSkip to conversation

checkpatch: add shebang check to EXECUTE_PERMISSIONS

RFC PATCH Superseded Ujjwal Kumar 3 participants 6 messages Oct 12, 2020 Checkpatch

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 checkpatch: add shebang check to EXECUTE_PERMISSIONS

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

There can be cases where the first line is not part of the patch. In that case there may be a false-positive report but in the end we will have less false-positives as we will be handling some of the unhandled cases.

---
Apologies, I forgot to include [email protected] so I'm now resending.

 scripts/checkpatch.pl | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/checkpatch.pl +19 -0
index fab38b493cef..e596d30794bf 100755
@@ -1795,6 +1795,23 @@ sub get_stat_here {
 	return $herectx;
 }
 
+sub get_shebang {
+	my ($linenr, $realfile) = @_;
+	my $rawline = "";
+	my $shebang = "";
+
+	$rawline = raw_line($linenr, 3);
+	if (defined $rawline &&
+		$rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
+		if (defined $1 && $1 == 1) {
+			$shebang = raw_line($linenr, 4);
+			$shebang = substr $shebang, 1;
+		}
+	}
+
+	return $shebang;
+}
+
 sub cat_vet {
 	my ($vet) = @_;
 	my ($res, $coded);
@@ -2680,7 +2697,9 @@ sub process {
 # Check for incorrect file permissions
 		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
 			my $permhere = $here . "FILE: $realfile\n";
+			my $shebang = get_shebang($linenr, $realfile);
 			if ($realfile !~ m@scripts/@ &&
+			    $shebang !~ /^#!\s*(\/\w)+.*/ &&
 			    $realfile !~ /\.(py|pl|awk|sh)$/) {
 				ERROR("EXECUTE_PERMISSIONS",
 				      "do not set execute permissions for source files\n" . $permhere);

base-commit: d67bc7812221606e1886620a357b13f906814af7 --
2.26.2

maintainer
#1 lore raw
Quoted 10 lines · Ujjwal Kumar

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Quoted 7 lines

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

There can be cases where the first line is not part of the patch.

For instance: a patch that only changes permissions without changing any of the file content.

scripts/checkpatch.pl

Quoted 14 lines

In that case there may be a false-positive report but in the end we will have less false-positives as we will be handling some of the unhandled cases.

---
Apologies, I forgot to include [email protected] so I'm now resending.

 scripts/checkpatch.pl | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub get_stat_here { · line 1796

Quoted 3 lines
scripts/checkpatch.pl +0 -0
@@ -1795,6 +1795,23 @@ sub get_stat_here {
 	return $herectx;
 }

First some style trivia:

scripts/checkpatch.pl sub get_stat_here { · line 1805

Quoted 8 lines
scripts/checkpatch.pl +8 -0
+sub get_shebang {
+	my ($linenr, $realfile) = @_;
+	my $rawline = "";
+	my $shebang = "";
+
+	$rawline = raw_line($linenr, 3);
+	if (defined $rawline &&
+		$rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {

alignment to open parenthesis please

scripts/checkpatch.pl sub get_stat_here { · line 1808

Quoted 3 lines
scripts/checkpatch.pl +3 -0
+		if (defined $1 && $1 == 1) {
+			$shebang = raw_line($linenr, 4);
+			$shebang = substr $shebang, 1;

parentheses around substr please.

scripts/checkpatch.pl sub get_stat_here { · line 1813

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+		}
+	}
+
+	return $shebang;
+}

And some real notes:

$realfile isn't used in this function so there doesn't seem to be a reason to have it as an function argument.

scripts/checkpatch.pl sub process { · line 2698

Quoted 7 lines
scripts/checkpatch.pl +1 -0
+
 sub cat_vet {
 	my ($vet) = @_;
 	my ($res, $coded);
@@ -2680,7 +2697,9 @@ sub process {
 # Check for incorrect file permissions
 		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {

probably better here to use a capture group for the permissions

		if ($line =~ /^new (?:file )?mode (\d+)$/) {
			my $mode = substr($1, -3);

scripts/checkpatch.pl sub process { · line 2701

Quoted 3 lines
scripts/checkpatch.pl +1 -0
 			my $permhere = $here . "FILE: $realfile\n";
+			my $shebang = get_shebang($linenr, $realfile);
 			if ($realfile !~ m@scripts/@ &&

Maybe remove the $realfile directory test as there are many source files that are not scripts in this directory and its subdirectories.

scripts/checkpatch.pl sub process { · line 2702

Quoted 1 line
scripts/checkpatch.pl +1 -0
+			    $shebang !~ /^#!\s*(\/\w)+.*/ &&

unnecessary capture group

and add

			   $mode =~ /[1357]/ &&

scripts/checkpatch.pl sub process { · line 2703

Quoted 1 line
scripts/checkpatch.pl +0 -0
 			    $realfile !~ /\.(py|pl|awk|sh)$/) {

No need for a a capture group here either. (existing defect)

Quoted 2 lines
scripts/checkpatch.pl +0 -0
 				ERROR("EXECUTE_PERMISSIONS",
 				      "do not set execute permissions for source files\n" . $permhere);
#2 lore raw
Quoted 81 lines · Joe Perches
Quoted 10 lines · Ujjwal Kumar

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Quoted 7 lines

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

There can be cases where the first line is not part of the patch.

For instance: a patch that only changes permissions without changing any of the file content.

scripts/checkpatch.pl

Quoted 14 lines

In that case there may be a false-positive report but in the end we will have less false-positives as we will be handling some of the unhandled cases.

---
Apologies, I forgot to include [email protected] so I'm now resending.

 scripts/checkpatch.pl | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub get_stat_here { · line 1796

Quoted 3 lines
scripts/checkpatch.pl +0 -0
@@ -1795,6 +1795,23 @@ sub get_stat_here {
 	return $herectx;
 }

First some style trivia:

scripts/checkpatch.pl sub get_stat_here { · line 1805

Quoted 8 lines
scripts/checkpatch.pl +8 -0
+sub get_shebang {
+	my ($linenr, $realfile) = @_;
+	my $rawline = "";
+	my $shebang = "";
+
+	$rawline = raw_line($linenr, 3);
+	if (defined $rawline &&
+		$rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {

alignment to open parenthesis please

scripts/checkpatch.pl sub get_stat_here { · line 1808

Quoted 3 lines
scripts/checkpatch.pl +3 -0
+		if (defined $1 && $1 == 1) {
+			$shebang = raw_line($linenr, 4);
+			$shebang = substr $shebang, 1;

parentheses around substr please.

scripts/checkpatch.pl sub get_stat_here { · line 1813

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+		}
+	}
+
+	return $shebang;
+}

And some real notes:

$realfile isn't used in this function so there doesn't seem to be a reason to have it as an function argument.

scripts/checkpatch.pl sub process { · line 2698

Quoted 7 lines
scripts/checkpatch.pl +1 -0
+
 sub cat_vet {
 	my ($vet) = @_;
 	my ($res, $coded);
@@ -2680,7 +2697,9 @@ sub process {
 # Check for incorrect file permissions
 		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {

probably better here to use a capture group for the permissions

Quoted 2 lines
		if ($line =~ /^new (?:file )?mode (\d+)$/) {
			my $mode = substr($1, -3);

This

Quoted 8 lines

scripts/checkpatch.pl sub process { · line 2701

Quoted 4 lines
scripts/checkpatch.pl +1 -0
 			my $permhere = $here . "FILE: $realfile\n";
+			my $shebang = get_shebang($linenr, $realfile);
 			if ($realfile !~ m@scripts/@ &&

Maybe remove the $realfile directory test as there are many source files that are not scripts in this directory and its subdirectories.

this

Quoted 8 lines

scripts/checkpatch.pl sub process { · line 2702

Quoted 2 lines
scripts/checkpatch.pl +1 -0
+			    $shebang !~ /^#!\s*(\/\w)+.*/ &&

unnecessary capture group

and add

			   $mode =~ /[1357]/ &&

this

Quoted 4 lines

scripts/checkpatch.pl sub process { · line 2703

Quoted 2 lines
scripts/checkpatch.pl +0 -0
 			    $realfile !~ /\.(py|pl|awk|sh)$/) {

No need for a a capture group here either. (existing defect)

and this.

Quoted 6 lines

scripts/checkpatch.pl sub process { · line 2705

Quoted 3 lines
scripts/checkpatch.pl +0 -0
 				ERROR("EXECUTE_PERMISSIONS",
 				      "do not set execute permissions for source files\n" . $permhere);

Should these new changes go as a separate patch or can they be included in the next iteration of this patch?

Thanks
Ujjwal Kumar
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
Quoted 117 lines · Ujjwal Kumar
Quoted 81 lines · Joe Perches
Quoted 10 lines · Ujjwal Kumar

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Quoted 7 lines

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

There can be cases where the first line is not part of the patch.

For instance: a patch that only changes permissions without changing any of the file content.

scripts/checkpatch.pl

Quoted 14 lines

In that case there may be a false-positive report but in the end we will have less false-positives as we will be handling some of the unhandled cases.

---
Apologies, I forgot to include [email protected] so I'm now resending.

 scripts/checkpatch.pl | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub get_stat_here { · line 1796

Quoted 3 lines
scripts/checkpatch.pl +0 -0
@@ -1795,6 +1795,23 @@ sub get_stat_here {
 	return $herectx;
 }

First some style trivia:

scripts/checkpatch.pl sub get_stat_here { · line 1805

Quoted 8 lines
scripts/checkpatch.pl +8 -0
+sub get_shebang {
+	my ($linenr, $realfile) = @_;
+	my $rawline = "";
+	my $shebang = "";
+
+	$rawline = raw_line($linenr, 3);
+	if (defined $rawline &&
+		$rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {

alignment to open parenthesis please

scripts/checkpatch.pl sub get_stat_here { · line 1808

Quoted 3 lines
scripts/checkpatch.pl +3 -0
+		if (defined $1 && $1 == 1) {
+			$shebang = raw_line($linenr, 4);
+			$shebang = substr $shebang, 1;

parentheses around substr please.

scripts/checkpatch.pl sub get_stat_here { · line 1813

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+		}
+	}
+
+	return $shebang;
+}

And some real notes:

$realfile isn't used in this function so there doesn't seem to be a reason to have it as an function argument.

scripts/checkpatch.pl sub process { · line 2698

Quoted 7 lines
scripts/checkpatch.pl +1 -0
+
 sub cat_vet {
 	my ($vet) = @_;
 	my ($res, $coded);
@@ -2680,7 +2697,9 @@ sub process {
 # Check for incorrect file permissions
 		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {

probably better here to use a capture group for the permissions

Quoted 2 lines
		if ($line =~ /^new (?:file )?mode (\d+)$/) {
			my $mode = substr($1, -3);

This

Quoted 8 lines

scripts/checkpatch.pl sub process { · line 2701

Quoted 4 lines
scripts/checkpatch.pl +1 -0
 			my $permhere = $here . "FILE: $realfile\n";
+			my $shebang = get_shebang($linenr, $realfile);
 			if ($realfile !~ m@scripts/@ &&

Maybe remove the $realfile directory test as there are many source files that are not scripts in this directory and its subdirectories.

this

Quoted 8 lines

scripts/checkpatch.pl sub process { · line 2702

Quoted 2 lines
scripts/checkpatch.pl +1 -0
+			    $shebang !~ /^#!\s*(\/\w)+.*/ &&

unnecessary capture group

and add

			   $mode =~ /[1357]/ &&

this

Quoted 4 lines

scripts/checkpatch.pl sub process { · line 2703

Quoted 2 lines
scripts/checkpatch.pl +0 -0
 			    $realfile !~ /\.(py|pl|awk|sh)$/) {

No need for a a capture group here either. (existing defect)

and this.

Quoted 1 line
Quoted 10 lines
Quoted 5 lines

scripts/checkpatch.pl sub process { · line 2705

Quoted 2 lines
scripts/checkpatch.pl +0 -0
 				ERROR("EXECUTE_PERMISSIONS",
 				      "do not set execute permissions for source files\n" . $permhere);

Should these new changes go as a separate patch or can they be included in the next iteration of this patch?

Ujjwal, please consider the following 'strategy':

- Send one patch to clean up the existing implementation as Joe requested.

With those 'credit points' for cleaning up the implementation, you then:

- Send another clean patch for the additional functionality you propose

We can probably easily accept the first cleanup, and then dig into the review of the additional functionality.

Lukas

maintainer
#5 lore raw
In reply to Ujjwal Kumar
Quoted 18 lines · Ujjwal Kumar
Quoted 17 lines · Joe Perches
Quoted 16 lines · Ujjwal Kumar

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

There can be cases where the first line is not part of the patch.

For instance: a patch that only changes permissions without changing any of the file content.

Please add verbiage like this to the commit message.

Quoted 2 lines

Should these new changes go as a separate patch or can they be included in the next iteration of this patch?

V2 please.

maintainer
#4 lore raw
In reply to Lukas Bulwahn
Quoted 19 lines · Lukas Bulwahn
Quoted 18 lines · Ujjwal Kumar
Quoted 17 lines · Joe Perches
Quoted 16 lines · Ujjwal Kumar

checkpatch.pl checks for invalid EXECUTE_PERMISSIONS on source files. The script leverages filename extensions and its path in the repository to decide whether to allow execute permissions on the file or not.

Based on current check conditions, a perl script file having execute permissions, without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.

Adding a shebang check along with current conditions will make the check more generalised and improve checkpatch reports. To do so, without breaking the core design decision of checkpatch, we can fetch the first line from the patch itself and match it for a shebang pattern.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

There can be cases where the first line is not part of the patch.

For instance: a patch that only changes permissions without changing any of the file content.

[]

Quoted 2 lines
Quoted 2 lines

Should these new changes go as a separate patch or can they be included in the next iteration of this patch?

[]
The commit log should be updated with the example shown. Please send a clean V2.