2021-10-03 22:38:05

by Vegard Nossum

[permalink] [raw]
Subject: [PATCH] x86/entry: fix AC assertion when !CONFIG_X86_SMAP

Commit 3c73b81a9164 ("x86/entry, selftests: Further improve user entry
sanity checks") added a warning if AC is set when in the kernel.

Commit 662a0221893a3d ("x86/entry: Fix AC assertion") changed the warning
to only fire if the CPU supports SMAP.

However, the warning can still trigger on a machine that supports SMAP but
where it's disabled in the kernel config:

[ T49] ------------[ cut here ]------------
[ T49] WARNING: CPU: 0 PID: 49 at irqentry_enter_from_user_mode+0x88/0xc0
[ T49] CPU: 0 PID: 49 Comm: init Tainted: G T 5.15.0-rc4+ #98 e6202628ee053b4f310759978284bd8bb0ce6905
[ T49] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
[ T49] RIP: 0010:irqentry_enter_from_user_mode+0x88/0xc0
[ T49] Code: eb 11 eb 03 90 90 c3 bf 01 00 00 00 e8 91 02 00 00 eb f1 eb 06 90 0f 0b 90 eb e7 65 8b 05 d8 22 b3 7e 83 f8 01 75 ee eb d9 90 <0f> 0b 90 f6 87 88 00 00 00 03 75 9b 90 0f 0b 90 eb 95 90 0f 0b 90
[ T49] RSP: 0000:ffff888004267eb0 EFLAGS: 00050006
[ T49] RAX: 0000000000044400 RBX: 0000000000000000 RCX: 0000000000000000
[ T49] RDX: 0000000000040006 RSI: 0000000000000008 RDI: ffff888004267f58
[ T49] RBP: ffff888004267f58 R08: ffff888004267f58 R09: 0000000000000000
[ T49] R10: 0000000000000000 R11: 0000000000000000 R12: ffffffff81600a15
[ T49] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000000000
[ T49] FS: 0000000000800000(0000) GS:ffff88803ec00000(0000) knlGS:0000000000000000
[ T49] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ T49] CR2: 00000000414bd008 CR3: 000000000426e003 CR4: 00000000001706f0
[ T49] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ T49] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ T49] Call Trace:
[ T49] ? irqentry_enter+0x27/0x40
[ T49] ? exc_general_protection+0x2b/0x2c0
[ T49] ? asm_exc_general_protection+0x5/0x20
[ T49] ? asm_exc_general_protection+0x1b/0x20
[ T49] ---[ end trace 9345e3548045cf79 ]---

We could add IS_ENABLED(CONFIG_X86_SMAP) to the warning condition, but
even this would not be enough in case SMAP is disabled at boot time with
the "nosmap" parameter.

To be consistent with "nosmap" behaviour, let's clear X86_FEATURE_SMAP
when !CONFIG_X86_SMAP.

I'm not sure if we should clear X86_CR4_SMAP when !CONFIG_X86_SMAP --
that's what the code currently does, so I've preserved that behaviour,
even though it seems a bit odd.

Found using entry-fuzz + satrandconfig.

Fixes: 3c73b81a9164 ("x86/entry, selftests: Further improve user entry sanity checks")
Fixes: 662a0221893a ("x86/entry: Fix AC assertion")
Cc: Peter Zijlstra (Intel) <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Daniel Thompson <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: [email protected]
Signed-off-by: Vegard Nossum <[email protected]>
---
arch/x86/kernel/cpu/common.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 0f8885949e8c4..b3410f1ac2175 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -326,6 +326,7 @@ static __always_inline void setup_smap(struct cpuinfo_x86 *c)
#ifdef CONFIG_X86_SMAP
cr4_set_bits(X86_CR4_SMAP);
#else
+ clear_cpu_cap(c, X86_FEATURE_SMAP);
cr4_clear_bits(X86_CR4_SMAP);
#endif
}
--
2.23.0.718.g5ad94255a8


2021-10-05 11:02:14

by tip-bot2 for Jacob Pan

[permalink] [raw]
Subject: [tip: x86/urgent] x86/entry: Clear X86_FEATURE_SMAP when CONFIG_X86_SMAP=n

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID: de85c350dde63d7f554d91c333d57ce6ad3bfc62
Gitweb: https://git.kernel.org/tip/de85c350dde63d7f554d91c333d57ce6ad3bfc62
Author: Vegard Nossum <[email protected]>
AuthorDate: Mon, 04 Oct 2021 00:34:23 +02:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Tue, 05 Oct 2021 12:37:45 +02:00

x86/entry: Clear X86_FEATURE_SMAP when CONFIG_X86_SMAP=n

Commit

3c73b81a9164 ("x86/entry, selftests: Further improve user entry sanity checks")

added a warning if AC is set when in the kernel.

Commit

662a0221893a3d ("x86/entry: Fix AC assertion")

changed the warning to only fire if the CPU supports SMAP.

However, the warning can still trigger on a machine that supports SMAP
but where it's disabled in the kernel config and when running the
syscall_nt selftest, for example:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 49 at irqentry_enter_from_user_mode
CPU: 0 PID: 49 Comm: init Tainted: G T 5.15.0-rc4+ #98 e6202628ee053b4f310759978284bd8bb0ce6905
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
RIP: 0010:irqentry_enter_from_user_mode
...
Call Trace:
? irqentry_enter
? exc_general_protection
? asm_exc_general_protection
? asm_exc_general_protectio

IS_ENABLED(CONFIG_X86_SMAP) could be added to the warning condition, but
even this would not be enough in case SMAP is disabled at boot time with
the "nosmap" parameter.

To be consistent with "nosmap" behaviour, clear X86_FEATURE_SMAP when
!CONFIG_X86_SMAP.

Found using entry-fuzz + satrandconfig.

[ bp: Massage commit message. ]

Fixes: 3c73b81a9164 ("x86/entry, selftests: Further improve user entry sanity checks")
Fixes: 662a0221893a ("x86/entry: Fix AC assertion")
Signed-off-by: Vegard Nossum <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Cc: [email protected]
Link: https://lkml.kernel.org/r/[email protected]
---
arch/x86/kernel/cpu/common.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 0f88859..b3410f1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -326,6 +326,7 @@ static __always_inline void setup_smap(struct cpuinfo_x86 *c)
#ifdef CONFIG_X86_SMAP
cr4_set_bits(X86_CR4_SMAP);
#else
+ clear_cpu_cap(c, X86_FEATURE_SMAP);
cr4_clear_bits(X86_CR4_SMAP);
#endif
}

2021-10-06 17:40:11

by tip-bot2 for Jacob Pan

[permalink] [raw]
Subject: [tip: x86/urgent] x86/entry: Clear X86_FEATURE_SMAP when CONFIG_X86_SMAP=n

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID: 3958b9c34c2729597e182cc606cc43942fd19f7c
Gitweb: https://git.kernel.org/tip/3958b9c34c2729597e182cc606cc43942fd19f7c
Author: Vegard Nossum <[email protected]>
AuthorDate: Mon, 04 Oct 2021 00:34:23 +02:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Wed, 06 Oct 2021 18:46:06 +02:00

x86/entry: Clear X86_FEATURE_SMAP when CONFIG_X86_SMAP=n

Commit

3c73b81a9164 ("x86/entry, selftests: Further improve user entry sanity checks")

added a warning if AC is set when in the kernel.

Commit

662a0221893a3d ("x86/entry: Fix AC assertion")

changed the warning to only fire if the CPU supports SMAP.

However, the warning can still trigger on a machine that supports SMAP
but where it's disabled in the kernel config and when running the
syscall_nt selftest, for example:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 49 at irqentry_enter_from_user_mode
CPU: 0 PID: 49 Comm: init Tainted: G T 5.15.0-rc4+ #98 e6202628ee053b4f310759978284bd8bb0ce6905
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
RIP: 0010:irqentry_enter_from_user_mode
...
Call Trace:
? irqentry_enter
? exc_general_protection
? asm_exc_general_protection
? asm_exc_general_protectio

IS_ENABLED(CONFIG_X86_SMAP) could be added to the warning condition, but
even this would not be enough in case SMAP is disabled at boot time with
the "nosmap" parameter.

To be consistent with "nosmap" behaviour, clear X86_FEATURE_SMAP when
!CONFIG_X86_SMAP.

Found using entry-fuzz + satrandconfig.

[ bp: Massage commit message. ]

Fixes: 3c73b81a9164 ("x86/entry, selftests: Further improve user entry sanity checks")
Fixes: 662a0221893a ("x86/entry: Fix AC assertion")
Signed-off-by: Vegard Nossum <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Cc: [email protected]
Link: https://lkml.kernel.org/r/[email protected]
---
arch/x86/kernel/cpu/common.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 0f88859..b3410f1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -326,6 +326,7 @@ static __always_inline void setup_smap(struct cpuinfo_x86 *c)
#ifdef CONFIG_X86_SMAP
cr4_set_bits(X86_CR4_SMAP);
#else
+ clear_cpu_cap(c, X86_FEATURE_SMAP);
cr4_clear_bits(X86_CR4_SMAP);
#endif
}