Re: module/taint: Automatically increase the buffer size for new taint flags

From: Jessica Yu
Date: Wed Sep 07 2016 - 21:00:20 EST


+++ Petr Mladek [07/09/16 15:13 +0200]:
The commit 66cc69e34e86a231 ("Fix: module signature vs tracepoints:
add new TAINT_UNSIGNED_MODULE") updated module_taint_flags() to
potentially print one more character. But it did not increase the
size of the corresponding buffers in m_show() and print_modules().

We have recently done the same mistake when adding a taint flag
for livepatching, see
https://lkml.kernel.org/g/cfba2c823bb984690b73572aaae1db596b54a082.1472137475.git.jpoimboe@xxxxxxxxxx

Let's convert the taint flags into enum and handle the buffer size
almost automatically.

It is not optimal because only few taint flags can be printed by
module_taint_flags(). But better be on the safe side. IMHO, it is
not worth the optimization and this is a good compromise.

Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
include/linux/kernel.h | 44 ++++++++++++++++++++++++--------------------
kernel/module.c | 8 ++++++--
kernel/panic.c | 4 ++--
3 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d96a6118d26a..1809bc82b7a5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -472,14 +472,10 @@ static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
if (panic_timeout == arch_default_timeout)
panic_timeout = timeout;
}
-extern const char *print_tainted(void);
enum lockdep_ok {
LOCKDEP_STILL_OK,
LOCKDEP_NOW_UNRELIABLE
};
-extern void add_taint(unsigned flag, enum lockdep_ok);
-extern int test_taint(unsigned flag);
-extern unsigned long get_taint(void);
extern int root_mountflags;

extern bool early_boot_irqs_disabled;
@@ -493,22 +489,30 @@ extern enum system_states {
SYSTEM_RESTART,
} system_state;

-#define TAINT_PROPRIETARY_MODULE 0
-#define TAINT_FORCED_MODULE 1
-#define TAINT_CPU_OUT_OF_SPEC 2
-#define TAINT_FORCED_RMMOD 3
-#define TAINT_MACHINE_CHECK 4
-#define TAINT_BAD_PAGE 5
-#define TAINT_USER 6
-#define TAINT_DIE 7
-#define TAINT_OVERRIDDEN_ACPI_TABLE 8
-#define TAINT_WARN 9
-#define TAINT_CRAP 10
-#define TAINT_FIRMWARE_WORKAROUND 11
-#define TAINT_OOT_MODULE 12
-#define TAINT_UNSIGNED_MODULE 13
-#define TAINT_SOFTLOCKUP 14
-#define TAINT_LIVEPATCH 15
+enum taint_flags {
+ TAINT_PROPRIETARY_MODULE, /* 0 */
+ TAINT_FORCED_MODULE, /* 1 */
+ TAINT_CPU_OUT_OF_SPEC, /* 2 */
+ TAINT_FORCED_RMMOD, /* 3 */
+ TAINT_MACHINE_CHECK, /* 4 */
+ TAINT_BAD_PAGE, /* 5 */
+ TAINT_USER, /* 6 */
+ TAINT_DIE, /* 7 */
+ TAINT_OVERRIDDEN_ACPI_TABLE, /* 8 */
+ TAINT_WARN, /* 9 */
+ TAINT_CRAP, /* 10 */
+ TAINT_FIRMWARE_WORKAROUND, /* 11 */
+ TAINT_OOT_MODULE, /* 12 */
+ TAINT_UNSIGNED_MODULE, /* 13 */
+ TAINT_SOFTLOCKUP, /* 14 */
+ TAINT_LIVEPATCH, /* 15 */
+ TAINT_FLAGS_COUNT /* keep last! */
+};

I liked the enum idea because we got TAINT_FLAGS_COUNT for free :-)
however I think we need to switch back to the #defines because of the kbuild
error.

The "Error: invalid operands...for `<<'" messages are related to the
__WARN_TAINT() macro (arch/arm64/include/asm/bug.h) which emits some assembly
that relies on the taint values. We don't have access to the enum values
in the assembler so we start getting things like:

.short ((1 << 0) | ((TAINT_WARN) << 8))

where TAINT_WARN should have already been preprocessed, and this is where that
invalid operand error is coming from.

Jessica