checkpatch: warn for use of %px
v2 cover checkpatch: warn for use of %px
Patch 1 and 2 are cleanup patches. Each adds a new subroutine to remove duplicate code.
Patch 3: Make checkpatch emit a warning for usage of specifier %px.
The initial idea to emit the warning was given on LKML by Andrew Morton, v1 requested permission to use 'Suggested-by' tag. This version does not add that tag.
thanks,
Tobin.
v2:
- change new sub name stat_real() -> get_stat_real()
- add new sub get_stat_here()
- (add new sub routines as separate patches)
- add 'Acked-by' tag for Joe
- remove 'Suggested-by' tag for Andrew Morton
Tobin C. Harding (3):
checkpatch: add sub routine get_stat_real()
checkpatch: add sub routine get_stat_here()
checkpatch: warn for use of %px
scripts/checkpatch.pl | 114 +++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 56 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/3 checkpatch: add sub routine get_stat_real()
checkpatch currently contains duplicate code. We can define a sub routine and call that instead. This reduces code duplication and line count.
Add subroutine get_stat_real()
--- scripts/checkpatch.pl | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
scripts/checkpatch.pl +15 -16
@@ -1612,6 +1612,17 @@ sub raw_line { return $line; } +sub get_stat_real { + my ($linenr, $lc) = @_; + + my $stat_real = raw_line($linenr, 0); + for (my $count = $linenr + 1; $count <= $lc; $count++) { + $stat_real = $stat_real . "\n" . raw_line($count, 0); + } + + return $stat_real; +} + sub cat_vet { my ($vet) = @_; my ($res, $coded);
@@ -5759,10 +5770,7 @@ sub process { } } if ($bad_extension ne "") { - my $stat_real = raw_line($linenr, 0); - for (my $count = $linenr + 1; $count <= $lc; $count++) { - $stat_real = $stat_real . "\n" . raw_line($count, 0); - } + my $stat_real = get_stat_real($linenr, $lc); WARN("VSPRINTF_POINTER_EXTENSION", "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n"); }
@@ -5877,10 +5885,7 @@ sub process { $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { my $lc = $stat =~ tr@\n@@; $lc = $lc + $linenr; - my $stat_real = raw_line($linenr, 0); - for (my $count = $linenr + 1; $count <= $lc; $count++) { - $stat_real = $stat_real . "\n" . raw_line($count, 0); - } + my $stat_real = get_stat_real($linenr, $lc); WARN("NAKED_SSCANF", "unchecked sscanf return value\n" . "$here\n$stat_real\n"); }
@@ -5891,10 +5896,7 @@ sub process { $line =~ /\bsscanf\b/) { my $lc = $stat =~ tr@\n@@; $lc = $lc + $linenr; - my $stat_real = raw_line($linenr, 0); - for (my $count = $linenr + 1; $count <= $lc; $count++) { - $stat_real = $stat_real . "\n" . raw_line($count, 0); - } + my $stat_real = get_stat_real($linenr, $lc); if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { my $format = $6; my $count = $format =~ tr@%@%@;
@@ -6289,10 +6291,7 @@ sub process { my $lc = $stat =~ tr@\n@@; $lc = $lc + $linenr; - my $stat_real = raw_line($linenr, 0); - for (my $count = $linenr + 1; $count <= $lc; $count++) { - $stat_real = $stat_real . "\n" . raw_line($count, 0); - } + my $stat_real = get_stat_real($linenr, $lc); my $skip_args = ""; if ($arg_pos > 1) {
Signature · 1 line
2.7.4
PATCH v2 2/3 checkpatch: add sub routine get_stat_here()
In reply to Tobin C. Hardingcheckpatch currently contains duplicate code. We can define a sub routine and call that instead. This reduces code duplication and line count.
Add subroutine get_stat_here()
--- scripts/checkpatch.pl | 54 +++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 34 deletions(-)
scripts/checkpatch.pl +20 -34
@@ -1623,6 +1623,17 @@ sub get_stat_real { return $stat_real; } +sub get_stat_here { + my ($linenr, $cnt, $here) = @_; + + my $herectx = $here . "\n"; + for (my $n = 0; $n < $cnt; $n++) { + $herectx .= raw_line($linenr, $n) . "\n"; + } + + return $herectx; +} + sub cat_vet { my ($vet) = @_; my ($res, $coded);
@@ -4911,12 +4922,8 @@ sub process { #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; $ctx =~ s/\n*$//; - my $herectx = $here . "\n"; my $stmt_cnt = statement_rawlines($ctx); - - for (my $n = 0; $n < $stmt_cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $stmt_cnt, $here); if ($dstat ne '' && $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
@@ -4988,12 +4995,9 @@ sub process { # check for macros with flow control, but without ## concatenation # ## concatenation is commonly a macro that defines a function so ignore those if ($has_flow_statement && !$has_arg_concat) { - my $herectx = $here . "\n"; my $cnt = statement_rawlines($ctx); + my $herectx = get_stat_here($linenr, $cnt, $here); - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } WARN("MACRO_WITH_FLOW_CONTROL", "Macros with flow control statements should be avoided\n" . "$herectx"); }
@@ -5033,11 +5037,7 @@ sub process { $ctx =~ s/\n*$//; my $cnt = statement_rawlines($ctx); - my $herectx = $here . "\n"; - - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $cnt, $here); if (($stmts =~ tr/;/;/) == 1 && $stmts !~ /^\s*(if|while|for|switch)\b/) {
@@ -5051,11 +5051,7 @@ sub process { } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { $ctx =~ s/\n*$//; my $cnt = statement_rawlines($ctx); - my $herectx = $here . "\n"; - - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $cnt, $here); WARN("TRAILING_SEMICOLON", "macros should not use a trailing semicolon\n" . "$herectx");
@@ -5178,12 +5174,8 @@ sub process { } } if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { - my $herectx = $here . "\n"; my $cnt = statement_rawlines($block); - - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $cnt, $here); WARN("BRACES", "braces {} are not necessary for single statement blocks\n" . $herectx);
@@ -6026,12 +6018,9 @@ sub process { } if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { - my $ctx = ''; - my $herectx = $here . "\n"; my $cnt = statement_rawlines($stat); - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $cnt, $here); + if (WARN("ALLOC_WITH_MULTIPLY", "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt == 1 &&
@@ -6114,12 +6103,9 @@ sub process { if ($^V && $^V ge 5.10.0 && defined $stat && $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { - my $ctx = ''; - my $herectx = $here . "\n"; my $cnt = statement_rawlines($stat); - for (my $n = 0; $n < $cnt; $n++) { - $herectx .= raw_line($linenr, $n) . "\n"; - } + my $herectx = get_stat_here($linenr, $cnt, $here); + WARN("DEFAULT_NO_BREAK", "switch default: should use break\n" . $herectx); }
Signature · 1 line
2.7.4
PATCH v2 3/3 checkpatch: warn for use of %px
In reply to Tobin C. HardingUsage of the new %px specifier potentially leaks sensitive inforamtion. Printing kernel addresses exposes the kernel layout in memory, this is potentially exploitable. We have tools in the kernel to help us do the right thing. We can have checkpatch warn developers of potential dangers of using %px.
Have checkpatch emit a warning for usage of specifier %px.
--- scripts/checkpatch.pl | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +24 -7
@@ -5750,21 +5750,38 @@ sub process { defined $stat && $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && $1 !~ /^_*volatile_*$/) { - my $bad_extension = ""; + my $specifier; + my $extension; + my $bad_specifier = ""; + my $stat_real; + my $lc = $stat =~ tr@\n@@; $lc = $lc + $linenr; for (my $count = $linenr; $count <= $lc; $count++) { my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); $fmt =~ s/%%//g; - if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) { - $bad_extension = $1; - last; + + while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { + $specifier = $1; + $extension = $2; + if ($extension !~ /[FfSsBKRraEhMmIiUDdgVCbGNOx]/) { + $bad_specifier = $specifier; + last; + } + if ($extension eq "x" && !defined($stat_real)) { + if (!defined($stat_real)) { + $stat_real = get_stat_real($linenr, $lc); + } + WARN("VSPRINTF_SPECIFIER_PX", + "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n"); + } } + } - if ($bad_extension ne "") { - my $stat_real = get_stat_real($linenr, $lc); + if ($bad_specifier ne "") { + $stat_real = get_stat_real($linenr, $lc); WARN("VSPRINTF_POINTER_EXTENSION", - "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n"); + "Invalid vsprintf pointer extension '$bad_specifier'\n" . "$here\n$stat_real\n"); } }
Signature · 1 line
2.7.4
Quoted 10 lines · Tobin C. Harding
Usage of the new %px specifier potentially leaks sensitive inforamtion. Printing kernel addresses exposes the kernel layout in memory, this is potentially exploitable. We have tools in the kernel to help us do the right thing. We can have checkpatch warn developers of potential dangers of using %px.
Have checkpatch emit a warning for usage of specifier %px.
Signed-off-by Tobin C. Harding <[email protected]>Co-developed-by Joe Perches <[email protected]>
Co-Developed-by:
Woops.
Joe I didn't quite understand what you meant when you said that this could go in via any tree. I'm still learning the whole kernel tree management thing. Don't checkpatch patches go in via Andy's tree?
https://git.kernel.org/pub/scm/linux/kernel/git/apw/checkpatch.git/
In any case, please drop this set. I'll fix the tag and re-spin.
thanks,
Tobin.
Quoted 58 lines
Acked-by Joe Perches <[email protected]>--- scripts/checkpatch.pl | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)scripts/checkpatch.pl +24
-7@@ -5750,21 +5750,38 @@ sub process { defined $stat && $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && $1 !~ /^_*volatile_*$/) { - my $bad_extension = ""; + my $specifier; + my $extension; + my $bad_specifier = ""; + my $stat_real; + my $lc = $stat =~ tr@\n@@; $lc = $lc + $linenr; for (my $count = $linenr; $count <= $lc; $count++) { my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); $fmt =~ s/%%//g; - if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) { - $bad_extension = $1; - last; + + while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { + $specifier = $1; + $extension = $2; + if ($extension !~ /[FfSsBKRraEhMmIiUDdgVCbGNOx]/) { + $bad_specifier = $specifier; + last; + } + if ($extension eq "x" && !defined($stat_real)) { + if (!defined($stat_real)) { + $stat_real = get_stat_real($linenr, $lc); + } + WARN("VSPRINTF_SPECIFIER_PX", + "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n"); + } } + } - if ($bad_extension ne "") { - my $stat_real = get_stat_real($linenr, $lc); + if ($bad_specifier ne "") { + $stat_real = get_stat_real($linenr, $lc); WARN("VSPRINTF_POINTER_EXTENSION", - "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n"); + "Invalid vsprintf pointer extension '$bad_specifier'\n" . "$here\n$stat_real\n"); } }Signature · 1 line
2.7.4
Quoted 14 lines · Tobin C. Harding
Quoted 10 lines · Tobin C. Harding
Usage of the new %px specifier potentially leaks sensitive inforamtion. Printing kernel addresses exposes the kernel layout in memory, this is potentially exploitable. We have tools in the kernel to help us do the right thing. We can have checkpatch warn developers of potential dangers of using %px.
Have checkpatch emit a warning for usage of specifier %px.
Signed-off-by Tobin C. Harding <[email protected]>Co-developed-by Joe Perches <[email protected]>Co-Developed-by:
Quoted 7 lines
Woops.
Joe I didn't quite understand what you meant when you said that this could go in via any tree. I'm still learning the whole kernel tree management thing. Don't checkpatch patches go in via Andy's tree?
https://git.kernel.org/pub/scm/linux/kernel/git/apw/checkpatch.git/
Oh, I see that tree is not super active. If there are no NACK's do you want me to put this through my tree? Perhaps once the Co-Developed-by stuff is in an acceptable form we could put them all together? Please do say if I'm going about things wrong, here to learn.
thanks,
Tobin.
Joe Perches
maintainerQuoted 21 lines · Tobin C. Harding
Quoted 20 lines · Tobin C. Harding
Quoted 10 lines · Tobin C. Harding
Usage of the new %px specifier potentially leaks sensitive inforamtion. Printing kernel addresses exposes the kernel layout in memory, this is potentially exploitable. We have tools in the kernel to help us do the right thing. We can have checkpatch warn developers of potential dangers of using %px.
Have checkpatch emit a warning for usage of specifier %px.
Signed-off-by Tobin C. Harding <[email protected]>Co-developed-by Joe Perches <[email protected]>Co-Developed-by:
Woops.
Joe I didn't quite understand what you meant when you said that this could go in via any tree. I'm still learning the whole kernel tree management thing. Don't checkpatch patches go in via Andy's tree?
Quoted 6 lines
Oh, I see that tree is not super active. If there are no NACK's do you want me to put this through my tree? Perhaps once the Co-Developed-by stuff is in an acceptable form we could put them all together? Please do say if I'm going about things wrong, here to learn.
No worries, you could push it through your tree.
btw: Here's an attempt at the case-sensitive signature matching
--- scripts/checkpatch.pl | 47 ++++++++++++++++++++++++------------------ ----- 1 file changed, 24 insertions(+), 23 deletions(-)
scripts/checkpatch.pl +24 -23
@@ -461,16 +461,18 @@ our $logFunctions = qr{(?x: seq_vprintf|seq_printf|seq_puts )}; -our $signature_tags = qr{(?xi: - Signed-off-by:| - Acked-by:| - Tested-by:| - Reviewed-by:| - Reported-by:| - Suggested-by:| - To:| - Cc: -)}; +our @valid_signatures = ( + "Signed-off-by:", + "Acked-by:", + "Tested-by:", + "Reviewed-by:", + "Reported-by:", + "Suggested-by:", + "Co-Developed-by:", + "To:", + "Cc:" +); +my $signature_tags = "(?x:" . join('|', @valid_signatures) . ")"; our @typeListMisordered = ( qr{char\s+(?:un)?signed},
@@ -2467,35 +2469,34 @@ sub process { my $sign_off = $2; my $space_after = $3; my $email = $4; - my $ucfirst_sign_off = ucfirst(lc($sign_off)); + my $preferred_sign_off = ucfirst(lc($sign_off)); - if ($sign_off !~ /$signature_tags/) { + if ($sign_off !~ /$signature_tags/i) { WARN("BAD_SIGN_OFF", "Non-standard signature: $sign_off\n" . $herecurr); - } - if (defined $space_before && $space_before ne "") { + } elsif ($sign_off !~ /$signature_tags/) { + $preferred_sign_off = $valid_signatures[ grep { $valid_signatures[$_] eq $sign_off } 0..$#valid_signatures ]; if (WARN("BAD_SIGN_OFF", - "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && + "'$preferred_sign_off' is the preferred signature form\n" . $herecurr) && $fix) { - $fixed[$fixlinenr] = - "$ucfirst_sign_off $email"; + $fixed[$fixlinenr] = "$preferred_sign_off $email"; } } - if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { + if (defined $space_before && $space_before ne "") { if (WARN("BAD_SIGN_OFF", - "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && + "Do not use whitespace before $preferred_sign_off\n" . $herecurr) && $fix) { $fixed[$fixlinenr] = - "$ucfirst_sign_off $email"; + "$preferred_sign_off $email"; } - } + if (!defined $space_after || $space_after ne " ") { if (WARN("BAD_SIGN_OFF", - "Use a single space after $ucfirst_sign_off\n" . $herecurr) && + "Use a single space after $preferred_sign_off\n" . $herecurr) && $fix) { $fixed[$fixlinenr] = - "$ucfirst_sign_off $email"; + "$preferred_sign_off $email"; } }
Joe Perches
maintainerQuoted 14 lines · Tobin C. Harding
Quoted 10 lines · Tobin C. Harding
Usage of the new %px specifier potentially leaks sensitive inforamtion. Printing kernel addresses exposes the kernel layout in memory, this is potentially exploitable. We have tools in the kernel to help us do the right thing. We can have checkpatch warn developers of potential dangers of using %px.
Have checkpatch emit a warning for usage of specifier %px.
Signed-off-by Tobin C. Harding <[email protected]>Co-developed-by Joe Perches <[email protected]>Co-Developed-by:
Quoted 7 lines
Woops.
Joe I didn't quite understand what you meant when you said that this could go in via any tree. I'm still learning the whole kernel tree management thing. Don't checkpatch patches go in via Andy's tree?
https://git.kernel.org/pub/scm/linux/kernel/git/apw/checkpatch.git/
No, not in quite awhile.
Most checkpatch patches go through Andrew Morton's quilt/mm tree.