Pull hardening updates from Kees Cook: - lib/string_choices: - Add str_up_down() helper (Michal Wajdeczko) - Add str_true_false()/str_false_true() helper (Hongbo Li) - Introduce several opposite string choice helpers (Hongbo Li) - lib/string_helpers: - rework overflow-dependent code (Justin Stitt) - fortify: refactor test_fortify Makefile to fix some build problems (Masahiro Yamada) - string: Check for "nonstring" attribute on strscpy() arguments - virt: vbox: Replace 1-element arrays with flexible arrays - media: venus: hfi_cmds: Replace 1-element arrays with flexible arrays * tag 'hardening-v6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: lib/string_choices: Add some comments to make more clear for string choices helpers. lib/string_choices: Introduce several opposite string choice helpers lib/string_choices: Add str_true_false()/str_false_true() helper string: Check for "nonstring" attribute on strscpy() arguments media: venus: hfi_cmds: struct hfi_session_release_buffer_pkt: Add __counted_by annotation media: venus: hfi_cmds: struct hfi_session_release_buffer_pkt: Replace 1-element array with flexible array virt: vbox: struct vmmdev_hgcm_pagelist: Replace 1-element array with flexible array lib/string_helpers: rework overflow-dependent code coccinelle: Add rules to find str_down_up() replacements string_choices: Add wrapper for str_down_up() coccinelle: Add rules to find str_up_down() replacements lib/string_choices: Add str_up_down() helper fortify: use if_changed_dep to record header dependency in *.cmd files fortify: move test_fortify.sh to lib/test_fortify/ fortify: refactor test_fortify Makefile to fix some build problems
Showing the changes to scripts (3 of 15 files). Show all
Unified · Split
| @@ -39,3 +39,49 @@ e << str_plural_r.E; · 38 unmodified lines | |||
| 39 | @@ | 39 | @@ |
| 40 | 40 | ||
| 41 | coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e) | 41 | coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e) |
| 42 | |||
| 43 | @str_up_down depends on patch@ | ||
| 44 | expression E; | ||
| 45 | @@ | ||
| 46 | ( | ||
| 47 | - ((E) ? "up" : "down") | ||
| 48 | + str_up_down(E) | ||
| 49 | ) | ||
| 50 | |||
| 51 | @str_up_down_r depends on !patch exists@ | ||
| 52 | expression E; | ||
| 53 | position P; | ||
| 54 | @@ | ||
| 55 | ( | ||
| 56 | * ((E@P) ? "up" : "down") | ||
| 57 | ) | ||
| 58 | |||
| 59 | @script:python depends on report@ | ||
| 60 | p << str_up_down_r.P; | ||
| 61 | e << str_up_down_r.E; | ||
| 62 | @@ | ||
| 63 | |||
| 64 | coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e) | ||
| 65 | |||
| 66 | @str_down_up depends on patch@ | ||
| 67 | expression E; | ||
| 68 | @@ | ||
| 69 | ( | ||
| 70 | - ((E) ? "down" : "up") | ||
| 71 | + str_down_up(E) | ||
| 72 | ) | ||
| 73 | |||
| 74 | @str_down_up_r depends on !patch exists@ | ||
| 75 | expression E; | ||
| 76 | position P; | ||
| 77 | @@ | ||
| 78 | ( | ||
| 79 | * ((E@P) ? "down" : "up") | ||
| 80 | ) | ||
| 81 | |||
| 82 | @script:python depends on report@ | ||
| 83 | p << str_down_up_r.P; | ||
| 84 | e << str_down_up_r.E; | ||
| 85 | @@ | ||
| 86 | |||
| 87 | coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e) | ||
| @@ -21,3 +21,5 @@ set -e · 20 unmodified lines | |||
| 21 | # then will be really dead and removed from the code base entirely. | 21 | # then will be really dead and removed from the code base entirely. |
| 22 | 22 | ||
| 23 | rm -f *.spec | 23 | rm -f *.spec |
| 24 | |||
| 25 | rm -f lib/test_fortify.log | ||
| @@ -1,66 +0,0 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 3 | set -e | ||
| 4 | |||
| 5 | # Argument 1: Source file to build. | ||
| 6 | IN="$1" | ||
| 7 | shift | ||
| 8 | # Extract just the filename for error messages below. | ||
| 9 | FILE="${IN##*/}" | ||
| 10 | # Extract the function name for error messages below. | ||
| 11 | FUNC="${FILE#*-}" | ||
| 12 | FUNC="${FUNC%%-*}" | ||
| 13 | FUNC="${FUNC%%.*}" | ||
| 14 | # Extract the symbol to test for in build/symbol test below. | ||
| 15 | WANT="__${FILE%%-*}" | ||
| 16 | |||
| 17 | # Argument 2: Where to write the build log. | ||
| 18 | OUT="$1" | ||
| 19 | shift | ||
| 20 | TMP="${OUT}.tmp" | ||
| 21 | |||
| 22 | # Argument 3: Path to "nm" tool. | ||
| 23 | NM="$1" | ||
| 24 | shift | ||
| 25 | |||
| 26 | # Remaining arguments are: $(CC) $(c_flags) | ||
| 27 | |||
| 28 | # Clean up temporary file at exit. | ||
| 29 | __cleanup() { | ||
| 30 | rm -f "$TMP" | ||
| 31 | } | ||
| 32 | trap __cleanup EXIT | ||
| 33 | |||
| 34 | # Function names in warnings are wrapped in backticks under UTF-8 locales. | ||
| 35 | # Run the commands with LANG=C so that grep output will not change. | ||
| 36 | export LANG=C | ||
| 37 | |||
| 38 | status= | ||
| 39 | # Attempt to build a source that is expected to fail with a specific warning. | ||
| 40 | if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then | ||
| 41 | # If the build succeeds, either the test has failed or the | ||
| 42 | # warning may only happen at link time (Clang). In that case, | ||
| 43 | # make sure the expected symbol is unresolved in the symbol list. | ||
| 44 | # If so, FORTIFY is working for this case. | ||
| 45 | if ! $NM -A "$OUT".o | grep -m1 "\bU ${WANT}$" >>"$TMP" ; then | ||
| 46 | status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN" | ||
| 47 | fi | ||
| 48 | else | ||
| 49 | # If the build failed, check for the warning in the stderr. | ||
| 50 | # GCC: | ||
| 51 | # ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning] | ||
| 52 | # Clang 14: | ||
| 53 | # ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] | ||
| 54 | if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then | ||
| 55 | status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN" | ||
| 56 | fi | ||
| 57 | fi | ||
| 58 | |||
| 59 | if [ -n "$status" ]; then | ||
| 60 | # Report on failure results, including compilation warnings. | ||
| 61 | echo "$status" | tee "$OUT" >&2 | ||
| 62 | else | ||
| 63 | # Report on good results, and save any compilation output to log. | ||
| 64 | echo "ok: unsafe ${FUNC}() usage correctly detected with '$WANT' in $IN" >"$OUT" | ||
| 65 | fi | ||
| 66 | cat "$TMP" >>"$OUT" | ||