checkpatch: add shebang check to EXECUTE_PERMISSIONS
RFC PATCH 1/1 checkpatch: add shebang check to EXECUTE_PERMISSIONS
checkpatch script 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 without '.pl' extension in its filename and not belonging to 'scripts/' directory is reported as ERROR which is a false-positive.
The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
--- scripts/checkpatch.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
scripts/checkpatch.pl +19 -0
@@ -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
Signature · 6 lines
2.26.2 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 14 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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 without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
Quoted 4 lines
Signed-off-by Ujjwal Kumar <[email protected]>--- scripts/checkpatch.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
To test the changes, I used the following scripts:
for i in $(find -executable -type f | grep "\.[a-z]" -v | grep "scripts/" -v); \
do \
hs=$(git log --reverse --format=%H $i | head -1); \
echo $hs; \
done | sort | uniq | \
xargs -n1 -P7 scripts/checkpatch.pl --show-types --types EXECUTE_PERMISSIONS --terse -g
Before patching, the script shows 21 false-positives. After applying the patch, the ERRORS are gone.
Thanks
Ujjwal Kumar
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 5 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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.
Quoted 9 lines
Based on current check conditions, a perl script file without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
I think one of the core design decisions of checkpatch.pl is:
checkpatch.pl can run on a patch, even if the patch does not apply to the current repository version that is checked out.
It solely uses the information in the patch, and does not try to guess how it could be applied etc.
This patch violates that core design decisions.
You can propose to Joe Perches and lkml, but do not be surprised if that is rejected because of this reason above.
I would be interested in the discussion.
Lukas
Quoted 44 lines
Signed-off-by Ujjwal Kumar <[email protected]>--- scripts/checkpatch.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)scripts/checkpatch.pl +19
-0@@ -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);
Quoted 5 lines
base-commit:
d67bc7812221606e1886620a357b13f906814af7Signature · 1 line
2.26.2
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 19 lines · Lukas Bulwahn
Quoted 5 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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.Quoted 9 lines
Based on current check conditions, a perl script file without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
Quoted 4 lines
I think one of the core design decisions of checkpatch.pl is:
checkpatch.pl can run on a patch, even if the patch does not apply to the current repository version that is checked out.
From our past conversation I remember about this particular point.
Quoted 3 lines
It solely uses the information in the patch, and does not try to guess how it could be applied etc.
I am fetching the 'shebang' from the patch itself (therefore I do not understand how does the proposed change violate that design decision?).
Quoted 2 lines
This patch violates that core design decisions.
Can you please point out the exact change that violates the design decisions?
Quoted 9 lines
You can propose to Joe Perches and lkml, but do not be surprised if that is rejected because of this reason above.
I would be interested in the discussion.
Lukas
Thanks
Ujjwal Kumar
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 25 lines · Ujjwal Kumar
Quoted 19 lines · Lukas Bulwahn
Quoted 5 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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.Quoted 9 lines
Based on current check conditions, a perl script file without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
Quoted 4 lines
I think one of the core design decisions of checkpatch.pl is:
checkpatch.pl can run on a patch, even if the patch does not apply to the current repository version that is checked out.
Quoted 9 lines
From our past conversation I remember about this particular point.
Quoted 3 lines
It solely uses the information in the patch, and does not try to guess how it could be applied etc.
I am fetching the 'shebang' from the patch itself (therefore I do not understand how does the proposed change violate that design decision?).
Okay, maybe I misread the patch; so, where those the first line come from? What if that first line is not part of the patch?
Quoted 9 lines
Quoted 2 lines
This patch violates that core design decisions.
Can you please point out the exact change that violates the design decisions?
Quoted 4 lines
You can propose to Joe Perches and lkml, but do not be surprised if that is rejected because of this reason above.
Quoted 9 lines
Quoted 5 lines
I would be interested in the discussion.
Lukas
Thanks
Ujjwal Kumar
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 35 lines · Lukas Bulwahn
Quoted 31 lines · Ujjwal Kumar
Quoted 19 lines · Lukas Bulwahn
Quoted 5 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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.Quoted 9 lines
Based on current check conditions, a perl script file without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
Quoted 4 lines
I think one of the core design decisions of checkpatch.pl is:
checkpatch.pl can run on a patch, even if the patch does not apply to the current repository version that is checked out.
From our past conversation I remember about this particular point.
Quoted 3 lines
It solely uses the information in the patch, and does not try to guess how it could be applied etc.
Quoted 6 lines
Quoted 3 lines
I am fetching the 'shebang' from the patch itself (therefore I do not understand how does the proposed change violate that design decision?).
Okay, maybe I misread the patch; so, where those the first line come from? What if that first line is not part of the patch?
In that case there might be a false-positive. But I tried to handle such cases where the first line is in the patch itself.
Okay, so there are total of 4 cases with file mode change:
1. mode change + create + modify
2. mode change + create
3. mode change + modify
4. mode change
the patch will handle all cases of type 1 and some cases of type 3. Other cases won't be handled because they cannot be handled without breaking the core design decision.
I do have references to say that the cases 2 and 4 are
Which I thought is an improvement over current logic used for the check.
Correct me if I am wrong.
Thanks
Ujjwal Kumar
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 53 lines · Ujjwal Kumar
Quoted 35 lines · Lukas Bulwahn
Quoted 31 lines · Ujjwal Kumar
Quoted 19 lines · Lukas Bulwahn
Quoted 5 lines · Ujjwal Kumar
checkpatch script checks for invalid
EXECUTE_PERMISSIONSon 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.Quoted 9 lines
Based on current check conditions, a perl script file without '.pl' extension in its filename and not belonging to '
scripts/' directory is reported as ERROR which is a false-positive.The script can correctly handle patches with mode changes and shebang line if shebang is taken into account. So, along with the current check conditions, adding the shebang check in the check conditions can improve the reports of the script.
Quoted 4 lines
I think one of the core design decisions of checkpatch.pl is:
checkpatch.pl can run on a patch, even if the patch does not apply to the current repository version that is checked out.
From our past conversation I remember about this particular point.
Quoted 3 lines
It solely uses the information in the patch, and does not try to guess how it could be applied etc.
Quoted 6 lines
Quoted 3 lines
I am fetching the 'shebang' from the patch itself (therefore I do not understand how does the proposed change violate that design decision?).
Okay, maybe I misread the patch; so, where those the first line come from? What if that first line is not part of the patch?
In that case there might be a false-positive. But I tried to handle such cases where the first line is in the patch itself.
Okay, so there are total of 4 cases with file mode change:
1. mode change + create + modify
2. mode change + create
3. mode change + modify
4. mode change
Quoted 8 lines
the patch will handle all cases of type 1 and some cases of type 3. Other cases won't be handled because they cannot be handled without breaking the core design decision.
I do have references to say that the cases 2 and 4 are
Which I thought is an improvement over current logic used for the check.
Sounds good, maybe you can explain that in the commit message and then send that to the mailing list and Joe Perches.
Lukas
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees