[PATCH v3 6/8] lockdown: bound the status string with scnprintf
From: Cai Xinchen
Date: Thu Nov 26 2026 - 07:47:38 EST
Use scnprintf() instead of sprintf() when composing the lockdown status
string in lockdown_read(). The current set of levels fits well within the
status buffer, but sprintf() provides no protection if more or longer
labels are added later. scnprintf() makes the buffer bounds explicit.
Store the level label strings in named arrays and point the matching
lockdown_reasons[] entries at them. Their sizes are compile-time
constants, so the status buffer itself can be sized for the string it
holds: all three level labels with their trailing spaces, the
brackets around the active one, and the terminating NUL. Whichever
level is active, the string has the same length. The label sizeof()
values include their own NULs, so only three more bytes are needed.
Suggested-by: Serge E. Hallyn <serge@xxxxxxxxxx>
Signed-off-by: Cai Xinchen <caixinchen1@xxxxxxxxxx>
---
security/lockdown/lockdown.c | 31 +++++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c
index 84efd533fe5a..239dbcdbabb4 100644
--- a/security/lockdown/lockdown.c
+++ b/security/lockdown/lockdown.c
@@ -14,13 +14,28 @@
#include <linux/lsm_hooks.h>
#include <uapi/linux/lsm.h>
+static const char lockdown_label_none[] = "none";
+static const char lockdown_label_integrity[] = "integrity";
+static const char lockdown_label_confidentiality[] = "confidentiality";
+
+/*
+ * The size of the lockdown status string: all three level labels
+ * with their trailing spaces, the brackets around the active one,
+ * and the terminating NUL. Whichever level is active, the string
+ * has the same length. The label sizeof() values include their
+ * own NULs, so only three more bytes are needed.
+ */
+#define LOCKDOWN_STATUS_LEN (sizeof(lockdown_label_none) + \
+ sizeof(lockdown_label_integrity) + \
+ sizeof(lockdown_label_confidentiality) + 3)
+
/*
* These are descriptions of the reasons that can be passed to the
* security_locked_down() LSM hook, used for the denial message and the
* securityfs status file.
*/
static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
- [LOCKDOWN_NONE] = "none",
+ [LOCKDOWN_NONE] = lockdown_label_none,
[LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
[LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
@@ -41,7 +56,7 @@ static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
[LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
[LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
[LOCKDOWN_XEN_USER_ACTIONS] = "Xen guest user action",
- [LOCKDOWN_INTEGRITY_MAX] = "integrity",
+ [LOCKDOWN_INTEGRITY_MAX] = lockdown_label_integrity,
[LOCKDOWN_KCORE] = "/proc/kcore access",
[LOCKDOWN_KPROBES] = "use of kprobes",
[LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
@@ -50,7 +65,7 @@ static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
[LOCKDOWN_TRACEFS] = "use of tracefs",
[LOCKDOWN_XMON_RW] = "xmon read and write access",
[LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
- [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
+ [LOCKDOWN_CONFIDENTIALITY_MAX] = lockdown_label_confidentiality,
};
static enum lockdown_reason kernel_locked_down;
@@ -134,7 +149,7 @@ static int __init lockdown_lsm_init(void)
static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
loff_t *ppos)
{
- char temp[80] = "";
+ char temp[LOCKDOWN_STATUS_LEN] = "";
int i, offset = 0;
for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
@@ -144,9 +159,13 @@ static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
const char *label = lockdown_reasons[level];
if (kernel_locked_down == level)
- offset += sprintf(temp+offset, "[%s] ", label);
+ offset += scnprintf(temp + offset,
+ sizeof(temp) - offset,
+ "[%s] ", label);
else
- offset += sprintf(temp+offset, "%s ", label);
+ offset += scnprintf(temp + offset,
+ sizeof(temp) - offset,
+ "%s ", label);
}
}
--
2.18.0.huawei.25