Skip to content

Commit 39b3f4e

Browse files
committed
Merge tag 'hardening-v6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
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
2 parents 667495d + c121d5c commit 39b3f4e

File tree

15 files changed

+134
-46
lines changed

15 files changed

+134
-46
lines changed

MAINTAINERS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8834,7 +8834,6 @@ F: include/linux/fortify-string.h
88348834
F: lib/fortify_kunit.c
88358835
F: lib/memcpy_kunit.c
88368836
F: lib/test_fortify/*
8837-
F: scripts/test_fortify.sh
88388837
K: \b__NO_FORTIFY\b
88398838

88408839
FPGA DFL DRIVERS

drivers/media/platform/qcom/venus/hfi_cmds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ struct hfi_session_release_buffer_pkt {
227227
u32 extradata_size;
228228
u32 response_req;
229229
u32 num_buffers;
230-
u32 buffer_info[1];
230+
u32 buffer_info[] __counted_by(num_buffers);
231231
};
232232

233233
struct hfi_session_release_resources_pkt {

include/linux/compiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ static inline void *offset_to_ptr(const int *off)
242242
/* &a[0] degrades to a pointer: a different type from an array */
243243
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
244244

245+
/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
246+
#define __must_be_cstr(p) BUILD_BUG_ON_ZERO(__annotated(p, nonstring))
247+
245248
/*
246249
* This returns a constant expression while determining if an argument is
247250
* a constant expression, most importantly without evaluating the argument.

include/linux/compiler_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ struct ftrace_likely_data {
421421
#define __member_size(p) __builtin_object_size(p, 1)
422422
#endif
423423

424+
/* Determine if an attribute has been applied to a variable. */
425+
#if __has_builtin(__builtin_has_attribute)
426+
#define __annotated(var, attr) __builtin_has_attribute(var, attr)
427+
#else
428+
#define __annotated(var, attr) (false)
429+
#endif
430+
424431
/*
425432
* Some versions of gcc do not mark 'asm goto' volatile:
426433
*

include/linux/string.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ ssize_t sized_strscpy(char *, const char *, size_t);
7676
* known size.
7777
*/
7878
#define __strscpy0(dst, src, ...) \
79-
sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
80-
#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
79+
sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) + \
80+
__must_be_cstr(dst) + __must_be_cstr(src))
81+
#define __strscpy1(dst, src, size) \
82+
sized_strscpy(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
8183

8284
#define __strscpy_pad0(dst, src, ...) \
83-
sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst))
84-
#define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size)
85+
sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst) + \
86+
__must_be_cstr(dst) + __must_be_cstr(src))
87+
#define __strscpy_pad1(dst, src, size) \
88+
sized_strscpy_pad(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
8589

8690
/**
8791
* strscpy - Copy a C-string into a sized buffer

include/linux/string_choices.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22
#ifndef _LINUX_STRING_CHOICES_H_
33
#define _LINUX_STRING_CHOICES_H_
44

5+
/*
6+
* Here provide a series of helpers in the str_$TRUE_$FALSE format (you can
7+
* also expand some helpers as needed), where $TRUE and $FALSE are their
8+
* corresponding literal strings. These helpers can be used in the printing
9+
* and also in other places where constant strings are required. Using these
10+
* helpers offers the following benefits:
11+
* 1) Reducing the hardcoding of strings, which makes the code more elegant
12+
* through these simple literal-meaning helpers.
13+
* 2) Unifying the output, which prevents the same string from being printed
14+
* in various forms, such as enable/disable, enabled/disabled, en/dis.
15+
* 3) Deduping by the linker, which results in a smaller binary file.
16+
*/
17+
518
#include <linux/types.h>
619

720
static inline const char *str_enable_disable(bool v)
821
{
922
return v ? "enable" : "disable";
1023
}
24+
#define str_disable_enable(v) str_enable_disable(!(v))
1125

1226
static inline const char *str_enabled_disabled(bool v)
1327
{
1428
return v ? "enabled" : "disabled";
1529
}
30+
#define str_disabled_enabled(v) str_enabled_disabled(!(v))
1631

1732
static inline const char *str_hi_lo(bool v)
1833
{
@@ -36,11 +51,25 @@ static inline const char *str_on_off(bool v)
3651
{
3752
return v ? "on" : "off";
3853
}
54+
#define str_off_on(v) str_on_off(!(v))
3955

4056
static inline const char *str_yes_no(bool v)
4157
{
4258
return v ? "yes" : "no";
4359
}
60+
#define str_no_yes(v) str_yes_no(!(v))
61+
62+
static inline const char *str_up_down(bool v)
63+
{
64+
return v ? "up" : "down";
65+
}
66+
#define str_down_up(v) str_up_down(!(v))
67+
68+
static inline const char *str_true_false(bool v)
69+
{
70+
return v ? "true" : "false";
71+
}
72+
#define str_false_true(v) str_true_false(!(v))
4473

4574
/**
4675
* str_plural - Return the simple pluralization based on English counts

include/uapi/linux/vbox_vmmdev_types.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ struct vmmdev_hgcm_pagelist {
282282
__u32 flags; /** VMMDEV_HGCM_F_PARM_*. */
283283
__u16 offset_first_page; /** Data offset in the first page. */
284284
__u16 page_count; /** Number of pages. */
285-
__u64 pages[1]; /** Page addresses. */
285+
union {
286+
__u64 unused; /** Deprecated place-holder for first "pages" entry. */
287+
__DECLARE_FLEX_ARRAY(__u64, pages); /** Page addresses. */
288+
};
286289
};
287290
VMMDEV_ASSERT_SIZE(vmmdev_hgcm_pagelist, 4 + 2 + 2 + 8);
288291

lib/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@
55
/gen_crc32table
66
/gen_crc64table
77
/oid_registry_data.c
8-
/test_fortify.log
9-
/test_fortify/*.log

lib/Makefile

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -393,40 +393,4 @@ obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
393393

394394
obj-$(CONFIG_FIRMWARE_TABLE) += fw_table.o
395395

396-
# FORTIFY_SOURCE compile-time behavior tests
397-
TEST_FORTIFY_SRCS = $(wildcard $(src)/test_fortify/*-*.c)
398-
TEST_FORTIFY_LOGS = $(patsubst $(src)/%.c, %.log, $(TEST_FORTIFY_SRCS))
399-
TEST_FORTIFY_LOG = test_fortify.log
400-
401-
quiet_cmd_test_fortify = TEST $@
402-
cmd_test_fortify = $(CONFIG_SHELL) $(srctree)/scripts/test_fortify.sh \
403-
$< $@ "$(NM)" $(CC) $(c_flags) \
404-
$(call cc-disable-warning,fortify-source) \
405-
-DKBUILD_EXTRA_WARN1
406-
407-
targets += $(TEST_FORTIFY_LOGS)
408-
clean-files += $(TEST_FORTIFY_LOGS)
409-
clean-files += $(addsuffix .o, $(TEST_FORTIFY_LOGS))
410-
$(obj)/test_fortify/%.log: $(src)/test_fortify/%.c \
411-
$(src)/test_fortify/test_fortify.h \
412-
$(srctree)/include/linux/fortify-string.h \
413-
$(srctree)/scripts/test_fortify.sh \
414-
FORCE
415-
$(call if_changed,test_fortify)
416-
417-
quiet_cmd_gen_fortify_log = GEN $@
418-
cmd_gen_fortify_log = cat </dev/null $(filter-out FORCE,$^) 2>/dev/null > $@ || true
419-
420-
targets += $(TEST_FORTIFY_LOG)
421-
clean-files += $(TEST_FORTIFY_LOG)
422-
$(obj)/$(TEST_FORTIFY_LOG): $(addprefix $(obj)/, $(TEST_FORTIFY_LOGS)) FORCE
423-
$(call if_changed,gen_fortify_log)
424-
425-
# Fake dependency to trigger the fortify tests.
426-
ifeq ($(CONFIG_FORTIFY_SOURCE),y)
427-
$(obj)/string.o: $(obj)/$(TEST_FORTIFY_LOG)
428-
endif
429-
430-
# Some architectures define __NO_FORTIFY if __SANITIZE_ADDRESS__ is undefined.
431-
# Pass CFLAGS_KASAN to avoid warnings.
432-
$(foreach x, $(patsubst %.log,%.o,$(TEST_FORTIFY_LOGS)), $(eval KASAN_SANITIZE_$(x) := y))
396+
subdir-$(CONFIG_FORTIFY_SOURCE) += test_fortify

lib/string_helpers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
321321
{
322322
char *out = dst;
323323

324+
if (!size)
325+
size = SIZE_MAX;
326+
324327
while (*src && --size) {
325328
if (src[0] == '\\' && src[1] != '\0' && size > 1) {
326329
src++;

0 commit comments

Comments
 (0)