* [PATCH] null_blk: refactor deprecated strncpy
@ 2023-09-11 21:46 Justin Stitt
2023-09-15 3:27 ` Kees Cook
0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2023-09-11 21:46 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, linux-kernel, linux-hardening, Kees Cook,
Nick Desaulniers, Nathan Chancellor, Justin Stitt
`strncpy` is deprecated for use on NUL-terminated destination strings [1].
We should favor a more robust and less ambiguous interface.
We know `nullb->disk_name` is NUL-terminated and we expect that
`disk->disk_name` also be NUL-terminated:
| snprintf(nullb->disk_name, sizeof(nullb->disk_name),
| "%s", config_item_name(&dev->group.cg_item));
It seems like NUL-padding may be required due to __assign_disk_name()
utilizing a memcpy as opposed to a `str*cpy` api.
| static inline void __assign_disk_name(char *name, struct gendisk *disk)
| {
| if (disk)
| memcpy(name, disk->disk_name, DISK_NAME_LEN);
| else
| memset(name, 0, DISK_NAME_LEN);
| }
This puzzles me as immediately after assigning the disk name we
go and print it with `__print_disk_name` which wraps `nullb_trace_disk_name()`.
| #define __print_disk_name(name) nullb_trace_disk_name(p, name)
This function obviously expects a NUL-terminated string.
| const char *nullb_trace_disk_name(struct trace_seq *p, char *name)
| {
| const char *ret = trace_seq_buffer_ptr(p);
|
| if (name && *name)
| trace_seq_printf(p, "disk=%s, ", name);
| trace_seq_putc(p, 0);
|
| return ret;
| }
I suspect that NUL-padding is _not_ needed but to be overly cautious
let's use `strscpy_pad` (although I strongly suspect that whole memcpy
thing should be changed).
Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested
---
drivers/block/null_blk/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index 864013019d6b..994919c63f05 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -1938,7 +1938,7 @@ static int null_gendisk_register(struct nullb *nullb)
else
disk->fops = &null_bio_ops;
disk->private_data = nullb;
- strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
+ strscpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
if (nullb->dev->zoned) {
int ret = null_register_zoned_dev(nullb);
---
base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
change-id: 20230911-strncpy-drivers-block-null_blk-main-c-7349153e1c6a
Best regards,
--
Justin Stitt <justinstitt@google.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] null_blk: refactor deprecated strncpy
2023-09-11 21:46 [PATCH] null_blk: refactor deprecated strncpy Justin Stitt
@ 2023-09-15 3:27 ` Kees Cook
0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2023-09-15 3:27 UTC (permalink / raw)
To: Justin Stitt
Cc: Jens Axboe, linux-block, linux-kernel, linux-hardening,
Nick Desaulniers, Nathan Chancellor
On Mon, Sep 11, 2023 at 09:46:47PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> We should favor a more robust and less ambiguous interface.
>
> We know `nullb->disk_name` is NUL-terminated and we expect that
> `disk->disk_name` also be NUL-terminated:
> | snprintf(nullb->disk_name, sizeof(nullb->disk_name),
> | "%s", config_item_name(&dev->group.cg_item));
More accurately, it's has uses that require a %NUL-terminated string:
pr_info("disk %s created\n", nullb->disk_name);
(i.e. showing the consumer's use is better evidence than the producer's
use.)
But I do think the above is good evidence that truncation is tolerated.
>
> It seems like NUL-padding may be required due to __assign_disk_name()
> utilizing a memcpy as opposed to a `str*cpy` api.
> | static inline void __assign_disk_name(char *name, struct gendisk *disk)
> | {
> | if (disk)
> | memcpy(name, disk->disk_name, DISK_NAME_LEN);
> | else
> | memset(name, 0, DISK_NAME_LEN);
> | }
I does look like it expects 0-fill. Looking at it with more context,
this appears to be a trace buffer:
TRACE_EVENT(nullb_zone_op,
TP_PROTO(struct nullb_cmd *cmd, unsigned int zone_no,
unsigned int zone_cond),
TP_ARGS(cmd, zone_no, zone_cond),
TP_STRUCT__entry(
__array(char, disk, DISK_NAME_LEN)
__field(enum req_op, op)
__field(unsigned int, zone_no)
__field(unsigned int, zone_cond)
),
TP_fast_assign(
__entry->op = req_op(cmd->rq);
__entry->zone_no = zone_no;
__entry->zone_cond = zone_cond;
__assign_disk_name(__entry->disk, cmd->rq->q->disk);
),
This should probably have been a dynamic string, but it's not. So let's
make sure this stays padded. Can you send this again but use
strscpy_pad() instead?
--
Kees Cook
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-09-15 3:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-11 21:46 [PATCH] null_blk: refactor deprecated strncpy Justin Stitt
2023-09-15 3:27 ` Kees Cook
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).