Skip to contentSkip to conversation

Fixes to linux-4.9.y stable tree swab.h and random initialization

v4.9 Superseded Hans-Christian Noren Egtvedt 2 participants 10 messages Jul 31, 2022 – Aug 2, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

I discovered one regression when I merged 4.9.320 for one of our platforms, the include of swab.h from user space (libudev) triggered the error described in https://lkml.org/lkml/2020/2/12/93

Cherry picking that commit solved the problem as expected. [v4.9 PATCH 1/6] include/uapi/linux/swab.h: fix userspace breakage,

Then the next part of this series:
[v4.9 PATCH 2/6] init/main: Fix double "the" in comment [v4.9 PATCH 3/6] init/main: properly align the multi-line comment [v4.9 PATCH 4/6] init: move stack canary initialization after [v4.9 PATCH 5/6] init/main.c: extract early boot entropy from the [v4.9 PATCH 6/6] random: move rand_initialize() earlier

Is to get the code flow similar in linux-4.9.y compared to HEAD implementation. I suspect there is a not completely ok merge conflict that was resolved during backport to linux-4.9.y branch with regards to the 'random: move rand_initialize() earlier' commit.

Now the code flow is in the same order as latest HEAD.

Signature · 1 line
Best regards, Hans-Christian Noren Egtvedt.
#1 lore raw

PATCH 1/6 include/uapi/linux/swab.h: fix userspace breakage, use __BITS_PER_LONG for swap

From: Christian Borntraeger <[email protected]>

QEMU has a funny new build error message when I use the upstream kernel headers:

CC block/file-posix.o
In file included from /home/cborntra/REPOS/qemu/include/qemu/timer.h:4,
from /home/cborntra/REPOS/qemu/include/qemu/timed-average.h:29,
from /home/cborntra/REPOS/qemu/include/block/accounting.h:28,
from /home/cborntra/REPOS/qemu/include/block/block_int.h:27,
from /home/cborntra/REPOS/qemu/block/file-posix.c:30:
/usr/include/linux/swab.h: In function `__swab':
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:34: error: "sizeof" is not defined, evaluates to 0 [-Werror=undef]
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^~~~~~
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:41: error: missing binary operator before token "("
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^
cc1: all warnings being treated as errors
make: *** [/home/cborntra/REPOS/qemu/rules.mak:69: block/file-posix.o] Error 1
rm tests/qemu-iotests/socket_scm_helper.o

This was triggered by commit d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h"). That patch is doing

  #include <asm/bitsperlong.h>

but it uses BITS_PER_LONG.

The kernel file asm/bitsperlong.h provide only __BITS_PER_LONG.

Let us use the __ variant in swap.h

Fixes d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h")
Cc Yury Norov <[email protected]>
Cc Allison Randal <[email protected]>
Cc Joe Perches <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc William Breathitt Gray <[email protected]>
Cc Torsten Hilbrich <[email protected]>
(cherry picked from commit 467d12f5c7842896d2de3ced74e4147ee29e97c8)
---
 include/uapi/linux/swab.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/uapi/linux/swab.h +2 -2
index 1f42d110987..51502eabdb0 100644
@@ -134,9 +134,9 @@ static inline __attribute_const__ __u32 __fswahb32(__u32 val)
 
 static __always_inline unsigned long __swab(const unsigned long y)
 {
-#if BITS_PER_LONG == 64
+#if __BITS_PER_LONG == 64
 	return __swab64(y);
-#else /* BITS_PER_LONG == 32 */
+#else /* __BITS_PER_LONG == 32 */
 	return __swab32(y);
 #endif
 }
Signature · 1 line
2.34.1
#2 lore raw

Re: [v4.9 PATCH 1/6] include/uapi/linux/swab.h: fix userspace breakage, use __BITS_PER_LONG for swap

Quoted 33 lines · Hans-Christian Noren Egtvedt

From: Christian Borntraeger <[email protected]>

QEMU has a funny new build error message when I use the upstream kernel headers:

CC block/file-posix.o
In file included from /home/cborntra/REPOS/qemu/include/qemu/timer.h:4,
from /home/cborntra/REPOS/qemu/include/qemu/timed-average.h:29,
from /home/cborntra/REPOS/qemu/include/block/accounting.h:28,
from /home/cborntra/REPOS/qemu/include/block/block_int.h:27,
from /home/cborntra/REPOS/qemu/block/file-posix.c:30:
/usr/include/linux/swab.h: In function `__swab':
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:34: error: "sizeof" is not defined, evaluates to 0 [-Werror=undef]
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^~~~~~
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:41: error: missing binary operator before token "("
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^
cc1: all warnings being treated as errors
make: *** [/home/cborntra/REPOS/qemu/rules.mak:69: block/file-posix.o] Error 1
rm tests/qemu-iotests/socket_scm_helper.o

This was triggered by commit d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h"). That patch is doing

  #include <asm/bitsperlong.h>

but it uses BITS_PER_LONG.

The kernel file asm/bitsperlong.h provide only __BITS_PER_LONG.

Let us use the __ variant in swap.h

Quoted 12 lines
Fixes d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h")
Cc Yury Norov <[email protected]>
Cc Allison Randal <[email protected]>
Cc Joe Perches <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc William Breathitt Gray <[email protected]>
Cc Torsten Hilbrich <[email protected]>
(cherry picked from commit 467d12f5c7842896d2de3ced74e4147ee29e97c8)

As you are passing on patches from others, it is usually requried that you too sign-off on them. Can you fix that up on this series and resend it so that I can take them?

thanks,

greg k-h

#3 lore raw

Re: [v4.9 PATCH 1/6] include/uapi/linux/swab.h: fix userspace breakage, use __BITS_PER_LONG for swap

Quoted 47 lines · Greg KH
Quoted 33 lines · Hans-Christian Noren Egtvedt

From: Christian Borntraeger <[email protected]>

QEMU has a funny new build error message when I use the upstream kernel headers:

CC block/file-posix.o
In file included from /home/cborntra/REPOS/qemu/include/qemu/timer.h:4,
from /home/cborntra/REPOS/qemu/include/qemu/timed-average.h:29,
from /home/cborntra/REPOS/qemu/include/block/accounting.h:28,
from /home/cborntra/REPOS/qemu/include/block/block_int.h:27,
from /home/cborntra/REPOS/qemu/block/file-posix.c:30:
/usr/include/linux/swab.h: In function `__swab':
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:34: error: "sizeof" is not defined, evaluates to 0 [-Werror=undef]
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^~~~~~
/home/cborntra/REPOS/qemu/include/qemu/bitops.h:20:41: error: missing binary operator before token "("
20 | #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
| ^
cc1: all warnings being treated as errors
make: *** [/home/cborntra/REPOS/qemu/rules.mak:69: block/file-posix.o] Error 1
rm tests/qemu-iotests/socket_scm_helper.o

This was triggered by commit d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h"). That patch is doing

  #include <asm/bitsperlong.h>

but it uses BITS_PER_LONG.

The kernel file asm/bitsperlong.h provide only __BITS_PER_LONG.

Let us use the __ variant in swap.h

Quoted 12 lines
Fixes d5767057c9a ("uapi: rename ext2_swab() to swab() and share globally in swab.h")
Cc Yury Norov <[email protected]>
Cc Allison Randal <[email protected]>
Cc Joe Perches <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc William Breathitt Gray <[email protected]>
Cc Torsten Hilbrich <[email protected]>
(cherry picked from commit 467d12f5c7842896d2de3ced74e4147ee29e97c8)
Quoted 3 lines

As you are passing on patches from others, it is usually requried that you too sign-off on them. Can you fix that up on this series and resend it so that I can take them?

Yes, sorry, we do not add signed-off-by entries internally, so my git config does not add it by default. Will resend the series.

Signature · 1 line
Best regards, Hans-Christian Noren Egtvedt
#4 lore raw

Re: [v4.9 PATCH 1/6] include/uapi/linux/swab.h: fix userspace breakage, use __BITS_PER_LONG for swap

Quoted 7 lines · Hans-Christian Egtvedt (hegtvedt)
Quoted 3 lines · Greg KH

As you are passing on patches from others, it is usually requried that you too sign-off on them. Can you fix that up on this series and resend it so that I can take them?

Yes, sorry, we do not add signed-off-by entries internally, so my git config does not add it by default. Will resend the series.

You really should do this internally as well, that way you have a "path of blame" to help keep track of why patches were added to your trees, and who did the reviewing of them.

I'll go pick up the resent series now, thanks.

greg k-h

#5 lore raw

PATCH 2/6 init/main: Fix double "the" in comment

In reply to Hans-Christian Noren Egtvedt

From: Viresh Kumar <[email protected]>

s/the\ the/the

(cherry picked from commit 6623f1c6150c09ce946c8e27a4c814d64919495b)
---
 init/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/main.c +1 -1
index 30226a836c8..a6ecdd492ed 100644
@@ -488,7 +488,7 @@ asmlinkage __visible void __init start_kernel(void)
 	debug_objects_early_init();
 
 	/*
-	 * Set up the the initial canary ASAP:
+	 * Set up the initial canary ASAP:
 	 */
 	add_latent_entropy();
 	boot_init_stack_canary();
Signature · 1 line
2.34.1
#6 lore raw

PATCH 3/6 init/main: properly align the multi-line comment

In reply to Hans-Christian Noren Egtvedt

From: Viresh Kumar <[email protected]>

Add a tab before it to follow standard practices. Also add the missing full stop '.'.

(cherry picked from commit 1b3b3b49b9961401331a1b496db5bec5c7b41ae6)
---
 init/main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
init/main.c +4 -4
index a6ecdd492ed..8b803078d7b 100644
@@ -498,10 +498,10 @@ asmlinkage __visible void __init start_kernel(void)
 	local_irq_disable();
 	early_boot_irqs_disabled = true;
 
-/*
- * Interrupts are still disabled. Do necessary setups, then
- * enable them
- */
+	/*
+	 * Interrupts are still disabled. Do necessary setups, then
+	 * enable them.
+	 */
 	boot_cpu_init();
 	page_address_init();
 	pr_notice("%s", linux_banner);
Signature · 1 line
2.34.1
#7 lore raw

PATCH 4/6 init: move stack canary initialization after setup_arch

In reply to Hans-Christian Noren Egtvedt

From: Laura Abbott <[email protected]>

Patch series "Command line randomness", v3.

A series to add the kernel command line as a source of randomness.

This patch (of 2):

Stack canary intialization involves getting a random number. Getting this random number may involve accessing caches or other architectural specific features which are not available until after the architecture is setup. Move the stack canary initialization later to accommodate this.

Cc "Theodore Ts'o" <[email protected]>
Cc Daniel Micay <[email protected]>
Cc Nick Kralevich <[email protected]>
(cherry picked from commit 121388a31362b0d3176dc1190ac8064b98a61b20)
---
 init/main.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
init/main.c +5 -6
index 8b803078d7b..67fa94c5967 100644
@@ -487,12 +487,6 @@ asmlinkage __visible void __init start_kernel(void)
 	smp_setup_processor_id();
 	debug_objects_early_init();
 
-	/*
-	 * Set up the initial canary ASAP:
-	 */
-	add_latent_entropy();
-	boot_init_stack_canary();
-
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -506,6 +500,11 @@ asmlinkage __visible void __init start_kernel(void)
 	page_address_init();
 	pr_notice("%s", linux_banner);
 	setup_arch(&command_line);
+	/*
+	 * Set up the the initial canary and entropy after arch
+	 */
+	add_latent_entropy();
+	boot_init_stack_canary();
 	mm_init_cpumask(&init_mm);
 	setup_command_line(command_line);
 	setup_nr_cpu_ids();
Signature · 1 line
2.34.1
#8 lore raw

PATCH 5/6 init/main.c: extract early boot entropy from the passed cmdline

In reply to Hans-Christian Noren Egtvedt

From: Daniel Micay <[email protected]>

Feed the boot command-line as to the /dev/random entropy pool

Existing Android bootloaders usually pass data which may not be known by an external attacker on the kernel command-line. It may also be the case on other embedded systems. Sample command-line from a Google Pixel running CopperheadOS....

    console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0
    androidboot.hardware=sailfish user_debug=31 ehci-hcd.park=3
    lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff buildvariant=user
    veritykeyid=id:dfcb9db0089e5b3b4090a592415c28e1cb4545ab
    androidboot.bootdevice=624000.ufshc androidboot.verifiedbootstate=yellow
    androidboot.veritymode=enforcing androidboot.keymaster=1
    androidboot.serialno=FA6CE0305299 androidboot.baseband=msm
    mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_samsung_ea8064tg_1080p_cmd:1:none:cfg:single_dsi
    androidboot.slot_suffix=_b fpsimd.fpsimd_settings=0
    app_setting.use_app_setting=0 kernelflag=0x00000000 debugflag=0x00000000
    androidboot.hardware.revision=PVT radioflag=0x00000000
    radioflagex1=0x00000000 radioflagex2=0x00000000 cpumask=0x00000000
    androidboot.hardware.ddr=4096MB,Hynix,LPDDR4 androidboot.ddrinfo=00000006
    androidboot.ddrsize=4GB androidboot.hardware.color=GRA00
    androidboot.hardware.ufs=32GB,Samsung androidboot.msm.hw_ver_id=268824801
    androidboot.qf.st=2 androidboot.cid=11111111 androidboot.mid=G-2PW4100
    androidboot.bootloader=8996-012001-1704121145
    androidboot.oem_unlock_support=1 androidboot.fp_src=1
    androidboot.htc.hrdump=detected androidboot.ramdump.opt=mem@2g:2g,mem@4g:2g
    androidboot.bootreason=reboot androidboot.ramdump_enable=0 ro
    root=/dev/dm-0 dm="system none ro,0 1 android-verity /dev/sda34"
    rootwait skip_initramfs init=/init androidboot.wificountrycode=US
    androidboot.boottime=1BLL:85,1BLE:669,2BLL:0,2BLE:1777,SW:6,KL:8136

Among other things, it contains a value unique to the device (androidboot.serialno=FA6CE0305299), unique to the OS builds for the device variant (veritykeyid=id:dfcb9db0089e5b3b4090a592415c28e1cb4545ab) and timings from the bootloader stages in milliseconds (androidboot.boottime=1BLL:85,1BLE:669,2BLL:0,2BLE:1777,SW:6,KL:8136).

[[email protected]: changelog tweak]
[[email protected]: line-wrapped command line]
Cc "Theodore Ts'o" <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Nick Kralevich <[email protected]>
(cherry picked from commit 33d72f3822d7ff8a9e45bd7413c811085cb87aa5)
---
 init/main.c | 2 ++
 1 file changed, 2 insertions(+)
init/main.c +2 -0
index 67fa94c5967..6bc1a3fa152 100644
@@ -502,8 +502,10 @@ asmlinkage __visible void __init start_kernel(void)
 	setup_arch(&command_line);
 	/*
 	 * Set up the the initial canary and entropy after arch
+	 * and after adding latent and command line entropy.
 	 */
 	add_latent_entropy();
+	add_device_randomness(command_line, strlen(command_line));
 	boot_init_stack_canary();
 	mm_init_cpumask(&init_mm);
 	setup_command_line(command_line);
Signature · 1 line
2.34.1
#9 lore raw

PATCH 6/6 random: move rand_initialize() earlier

In reply to Hans-Christian Noren Egtvedt

From: Kees Cook <[email protected]>

Right now rand_initialize() is run as an early_initcall(), but it only depends on timekeeping_init() (for mixing ktime_get_real() into the pools). However, the call to boot_init_stack_canary() for stack canary initialization runs earlier, which triggers a warning at boot:

random: get_random_bytes called from start_kernel+0x357/0x548 with crng_init=0

Instead, this moves rand_initialize() to after timekeeping_init(), and moves canary initialization here as well.

Note that this warning may still remain for machines that do not have UEFI RNG support (which initializes the RNG pools during setup_arch()), or for x86 machines without RDRAND (or booting without "random.trust=on" or CONFIG_RANDOM_TRUST_CPU=y).

(cherry picked from commit d55535232c3dbde9a523a9d10d68670f5fe5dec3)

Adjusted to fit on top of linux-4.9.y branch, suspecting a wrongly solved conflict when cherry picked earlier.

---
 init/main.c | 7 -------
 1 file changed, 7 deletions(-)
init/main.c +0 -7
index 6bc1a3fa152..6537f51a0ba 100644
@@ -500,13 +500,6 @@ asmlinkage __visible void __init start_kernel(void)
 	page_address_init();
 	pr_notice("%s", linux_banner);
 	setup_arch(&command_line);
-	/*
-	 * Set up the the initial canary and entropy after arch
-	 * and after adding latent and command line entropy.
-	 */
-	add_latent_entropy();
-	add_device_randomness(command_line, strlen(command_line));
-	boot_init_stack_canary();
 	mm_init_cpumask(&init_mm);
 	setup_command_line(command_line);
 	setup_nr_cpu_ids();
Signature · 1 line
2.34.1