Skip to content

Commit 2dc26d9

Browse files
committed
Merge tag 'overflow-v5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull overflow updates from Kees Cook: "The end goal of the current buffer overflow detection work[0] is to gain full compile-time and run-time coverage of all detectable buffer overflows seen via array indexing or memcpy(), memmove(), and memset(). The str*() family of functions already have full coverage. While much of the work for these changes have been on-going for many releases (i.e. 0-element and 1-element array replacements, as well as avoiding false positives and fixing discovered overflows[1]), this series contains the foundational elements of several related buffer overflow detection improvements by providing new common helpers and FORTIFY_SOURCE changes needed to gain the introspection required for compiler visibility into array sizes. Also included are a handful of already Acked instances using the helpers (or related clean-ups), with many more waiting at the ready to be taken via subsystem-specific trees[2]. The new helpers are: - struct_group() for gaining struct member range introspection - memset_after() and memset_startat() for clearing to the end of structures - DECLARE_FLEX_ARRAY() for using flex arrays in unions or alone in structs Also included is the beginning of the refactoring of FORTIFY_SOURCE to support memcpy() introspection, fix missing and regressed coverage under GCC, and to prepare to fix the currently broken Clang support. Finishing this work is part of the larger series[0], but depends on all the false positives and buffer overflow bug fixes to have landed already and those that depend on this series to land. As part of the FORTIFY_SOURCE refactoring, a set of both a compile-time and run-time tests are added for FORTIFY_SOURCE and the mem*()-family functions respectively. The compile time tests have found a legitimate (though corner-case) bug[6] already. Please note that the appearance of "panic" and "BUG" in the FORTIFY_SOURCE refactoring are the result of relocating existing code, and no new use of those code-paths are expected nor desired. Finally, there are two tree-wide conversions for 0-element arrays and flexible array unions to gain sane compiler introspection coverage that result in no known object code differences. After this series (and the changes that have now landed via netdev and usb), we are very close to finally being able to build with -Warray-bounds and -Wzero-length-bounds. However, due corner cases in GCC[3] and Clang[4], I have not included the last two patches that turn on these options, as I don't want to introduce any known warnings to the build. Hopefully these can be solved soon" Link: https://lore.kernel.org/lkml/[email protected]/ [0] Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/?qt=grep&q=FORTIFY_SOURCE [1] Link: https://lore.kernel.org/lkml/202108220107.3E26FE6C9C@keescook/ [2] Link: https://lore.kernel.org/lkml/[email protected]/ [3] Link: https://bugs.llvm.org/show_bug.cgi?id=51682 [4] Link: https://lore.kernel.org/lkml/202109051257.29B29745C0@keescook/ [5] Link: https://lore.kernel.org/lkml/[email protected]/ [6] * tag 'overflow-v5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: (30 commits) fortify: strlen: Avoid shadowing previous locals compiler-gcc.h: Define __SANITIZE_ADDRESS__ under hwaddress sanitizer treewide: Replace 0-element memcpy() destinations with flexible arrays treewide: Replace open-coded flex arrays in unions stddef: Introduce DECLARE_FLEX_ARRAY() helper btrfs: Use memset_startat() to clear end of struct string.h: Introduce memset_startat() for wiping trailing members and padding xfrm: Use memset_after() to clear padding string.h: Introduce memset_after() for wiping trailing members/padding lib: Introduce CONFIG_MEMCPY_KUNIT_TEST fortify: Add compile-time FORTIFY_SOURCE tests fortify: Allow strlen() and strnlen() to pass compile-time known lengths fortify: Prepare to improve strnlen() and strlen() warnings fortify: Fix dropped strcpy() compile-time write overflow check fortify: Explicitly disable Clang support fortify: Move remaining fortify helpers into fortify-string.h lib/string: Move helper functions out of string.c compiler_types.h: Remove __compiletime_object_size() cm4000_cs: Use struct_group() to zero struct cm4000_dev region can: flexcan: Use struct_group() to zero struct flexcan_regs regions ...
2 parents f594e28 + 95cadae commit 2dc26d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1160
-446
lines changed

MAINTAINERS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7341,6 +7341,15 @@ L: [email protected]
73417341
S: Maintained
73427342
F: drivers/net/ethernet/nvidia/*
73437343

7344+
FORTIFY_SOURCE
7345+
M: Kees Cook <[email protected]>
7346+
7347+
S: Supported
7348+
F: include/linux/fortify-string.h
7349+
F: lib/test_fortify/*
7350+
F: scripts/test_fortify.sh
7351+
K: \b__NO_FORTIFY\b
7352+
73447353
FPGA DFL DRIVERS
73457354
M: Wu Hao <[email protected]>
73467355
R: Tom Rix <[email protected]>

arch/arm/boot/compressed/string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Small subset of simple string routines
66
*/
77

8+
#define __NO_FORTIFY
89
#include <linux/string.h>
910

1011
/*

arch/s390/lib/string.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99

1010
#define IN_ARCH_STRING_C 1
11+
#ifndef __NO_FORTIFY
12+
# define __NO_FORTIFY
13+
#endif
1114

1215
#include <linux/types.h>
1316
#include <linux/string.h>

arch/x86/boot/compressed/misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#undef CONFIG_KASAN
1515
#undef CONFIG_KASAN_GENERIC
1616

17+
#define __NO_FORTIFY
18+
1719
/* cpu_feature_enabled() cannot be used this early */
1820
#define USE_EARLY_PGTABLE_L5
1921

arch/x86/boot/compressed/pgtable_64.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "misc.h"
13
#include <linux/efi.h>
24
#include <asm/e820/types.h>
35
#include <asm/processor.h>

arch/x86/lib/string_32.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* strings.
1212
*/
1313

14+
#define __NO_FORTIFY
1415
#include <linux/string.h>
1516
#include <linux/export.h>
1617

drivers/char/pcmcia/cm4000_cs.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ struct cm4000_dev {
116116
wait_queue_head_t atrq; /* wait for ATR valid */
117117
wait_queue_head_t readq; /* used by write to wake blk.read */
118118

119-
/* warning: do not move this fields.
119+
/* warning: do not move this struct group.
120120
* initialising to zero depends on it - see ZERO_DEV below. */
121+
struct_group(init,
121122
unsigned char atr_csum;
122123
unsigned char atr_len_retry;
123124
unsigned short atr_len;
@@ -140,12 +141,10 @@ struct cm4000_dev {
140141

141142
struct timer_list timer; /* used to keep monitor running */
142143
int monitor_running;
144+
);
143145
};
144146

145-
#define ZERO_DEV(dev) \
146-
memset(&dev->atr_csum,0, \
147-
sizeof(struct cm4000_dev) - \
148-
offsetof(struct cm4000_dev, atr_csum))
147+
#define ZERO_DEV(dev) memset(&((dev)->init), 0, sizeof((dev)->init))
149148

150149
static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
151150
static struct class *cmm_class;

drivers/crypto/chelsio/chcr_crypto.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,10 @@ struct chcr_authenc_ctx {
222222
};
223223

224224
struct __aead_ctx {
225-
struct chcr_gcm_ctx gcm[0];
226-
struct chcr_authenc_ctx authenc[];
225+
union {
226+
DECLARE_FLEX_ARRAY(struct chcr_gcm_ctx, gcm);
227+
DECLARE_FLEX_ARRAY(struct chcr_authenc_ctx, authenc);
228+
};
227229
};
228230

229231
struct chcr_aead_ctx {
@@ -245,9 +247,11 @@ struct hmac_ctx {
245247
};
246248

247249
struct __crypto_ctx {
248-
struct hmac_ctx hmacctx[0];
249-
struct ablk_ctx ablkctx[0];
250-
struct chcr_aead_ctx aeadctx[];
250+
union {
251+
DECLARE_FLEX_ARRAY(struct hmac_ctx, hmacctx);
252+
DECLARE_FLEX_ARRAY(struct ablk_ctx, ablkctx);
253+
DECLARE_FLEX_ARRAY(struct chcr_aead_ctx, aeadctx);
254+
};
251255
};
252256

253257
struct chcr_context {

drivers/cxl/cxl.h

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -75,52 +75,27 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr)
7575
#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
7676
#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20
7777

78-
#define CXL_COMPONENT_REGS() \
79-
void __iomem *hdm_decoder
80-
81-
#define CXL_DEVICE_REGS() \
82-
void __iomem *status; \
83-
void __iomem *mbox; \
84-
void __iomem *memdev
85-
86-
/* See note for 'struct cxl_regs' for the rationale of this organization */
87-
/*
88-
* CXL_COMPONENT_REGS - Common set of CXL Component register block base pointers
89-
* @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
90-
*/
91-
struct cxl_component_regs {
92-
CXL_COMPONENT_REGS();
93-
};
94-
95-
/* See note for 'struct cxl_regs' for the rationale of this organization */
96-
/*
97-
* CXL_DEVICE_REGS - Common set of CXL Device register block base pointers
98-
* @status: CXL 2.0 8.2.8.3 Device Status Registers
99-
* @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
100-
* @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
101-
*/
102-
struct cxl_device_regs {
103-
CXL_DEVICE_REGS();
104-
};
105-
10678
/*
107-
* Note, the anonymous union organization allows for per
108-
* register-block-type helper routines, without requiring block-type
109-
* agnostic code to include the prefix.
79+
* Using struct_group() allows for per register-block-type helper routines,
80+
* without requiring block-type agnostic code to include the prefix.
11081
*/
11182
struct cxl_regs {
112-
union {
113-
struct {
114-
CXL_COMPONENT_REGS();
115-
};
116-
struct cxl_component_regs component;
117-
};
118-
union {
119-
struct {
120-
CXL_DEVICE_REGS();
121-
};
122-
struct cxl_device_regs device_regs;
123-
};
83+
/*
84+
* Common set of CXL Component register block base pointers
85+
* @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
86+
*/
87+
struct_group_tagged(cxl_component_regs, component,
88+
void __iomem *hdm_decoder;
89+
);
90+
/*
91+
* Common set of CXL Device register block base pointers
92+
* @status: CXL 2.0 8.2.8.3 Device Status Registers
93+
* @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
94+
* @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
95+
*/
96+
struct_group_tagged(cxl_device_regs, device_regs,
97+
void __iomem *status, *mbox, *memdev;
98+
);
12499
};
125100

126101
struct cxl_reg_map {

drivers/gpu/drm/mga/mga_ioc32.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@
3838
typedef struct drm32_mga_init {
3939
int func;
4040
u32 sarea_priv_offset;
41-
int chipset;
42-
int sgram;
43-
unsigned int maccess;
44-
unsigned int fb_cpp;
45-
unsigned int front_offset, front_pitch;
46-
unsigned int back_offset, back_pitch;
47-
unsigned int depth_cpp;
48-
unsigned int depth_offset, depth_pitch;
49-
unsigned int texture_offset[MGA_NR_TEX_HEAPS];
50-
unsigned int texture_size[MGA_NR_TEX_HEAPS];
41+
struct_group(always32bit,
42+
int chipset;
43+
int sgram;
44+
unsigned int maccess;
45+
unsigned int fb_cpp;
46+
unsigned int front_offset, front_pitch;
47+
unsigned int back_offset, back_pitch;
48+
unsigned int depth_cpp;
49+
unsigned int depth_offset, depth_pitch;
50+
unsigned int texture_offset[MGA_NR_TEX_HEAPS];
51+
unsigned int texture_size[MGA_NR_TEX_HEAPS];
52+
);
5153
u32 fb_offset;
5254
u32 mmio_offset;
5355
u32 status_offset;
@@ -67,9 +69,8 @@ static int compat_mga_init(struct file *file, unsigned int cmd,
6769

6870
init.func = init32.func;
6971
init.sarea_priv_offset = init32.sarea_priv_offset;
70-
memcpy(&init.chipset, &init32.chipset,
71-
offsetof(drm_mga_init_t, fb_offset) -
72-
offsetof(drm_mga_init_t, chipset));
72+
memcpy(&init.always32bit, &init32.always32bit,
73+
sizeof(init32.always32bit));
7374
init.fb_offset = init32.fb_offset;
7475
init.mmio_offset = init32.mmio_offset;
7576
init.status_offset = init32.status_offset;

0 commit comments

Comments
 (0)