[PATCH v3 6/8] lockdown: bound the status string with scnprintf

sashiko-bot at kernel.org sashiko-bot at kernel.org
Thu Nov 26 12:52:43 UTC 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] lockdown: status string truncation in lockdown_read() on
  concurrent write
--

commit 66eac39b9c466fbdeff2afcfbf4a18a53ab7e3f1
Author: Cai Xinchen <caixinchen1 at huawei.com>

lockdown: bound the status string with scnprintf

This commit replaces sprintf() with scnprintf() in lockdown_read() and
sizes the status buffer to the exact length of the three level labels with
one bracketed active level.

> diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c
> index 84efd533fe5a6..239dbcdbabb40 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)

[ ... ]

> @@ -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);
>  		}
>  	}

[Severity: Low]
Can a concurrent lockdown_write() cause lockdown_read() to truncate the
status string in temp[]?

LOCKDOWN_STATUS_LEN (34 bytes) only leaves room for one pair of brackets,
and kernel_locked_down is re-read on each iteration of the loop:

CPU0                              CPU1
lockdown_read()
  i = 0 (LOCKDOWN_NONE):
    kernel_locked_down == level
    scnprintf(..., "[none] ")
                                  lockdown_write()
                                    lock_kernel_down(...,
                                      LOCKDOWN_INTEGRITY_MAX)
  i = 1 (LOCKDOWN_INTEGRITY_MAX):
    kernel_locked_down == level
    scnprintf(..., "[integrity] ")
  i = 2 (LOCKDOWN_CONFIDENTIALITY_MAX):
    scnprintf(..., "confidentiality ")
      <-- truncated to "confidentialit"

temp[offset - 1] = '\n' then overwrites the last character of the label,
returning "[none] [integrity] confidentiali\n" to userspace.  Reading
kernel_locked_down once with READ_ONCE() before the loop would avoid
matching more than one level.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126131315.1256553-1-caixinchen1@huawei.com?part=6



More information about the Linux-security-module-archive mailing list