From 441622af05ab8f9aaa5395744e435fd2ad97fef2 Mon Sep 17 00:00:00 2001 From: Rajat Chopra Date: Mon, 20 Jul 2026 17:12:21 -0700 Subject: [PATCH 1/2] safe strcpy driver root Use strlcpy for safer copy of bytes. The number of bytes are provided in the argument. If ctx-root is underallocated, then we should quit with an error. Signed-off-by: Rajat Chopra --- src/driver.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/driver.c b/src/driver.c index d4e91c037..659b4debd 100644 --- a/src/driver.c +++ b/src/driver.c @@ -3,7 +3,7 @@ */ #include - +#include #include #include "nvml.h" @@ -89,7 +89,10 @@ driver_init(struct error *err, struct dxcore_context *dxcore, const char *root, .gid = gid, .nvml_dl = NULL, }; - strcpy(ctx->root, root); + if (strlcpy(ctx->root, root, sizeof(ctx->root)) >= sizeof(ctx->root)) { + error_setx(err, "root path too long (max %zu bytes)", sizeof(ctx->root) - 1); + goto fail; + } if (dxcore->initialized) { memset(ctx->nvml_path, 0, strlen(ctx->nvml_path)); From 9c7eb9903429a92a4ff5e0881ac6049ba8b23589 Mon Sep 17 00:00:00 2001 From: Rajat Chopra Date: Fri, 7 Aug 2026 13:51:45 -0700 Subject: [PATCH 2/2] Use length check instead of strlcpy --- src/driver.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/driver.c b/src/driver.c index 659b4debd..ea84d5ab2 100644 --- a/src/driver.c +++ b/src/driver.c @@ -3,7 +3,6 @@ */ #include -#include #include #include "nvml.h" @@ -89,10 +88,11 @@ driver_init(struct error *err, struct dxcore_context *dxcore, const char *root, .gid = gid, .nvml_dl = NULL, }; - if (strlcpy(ctx->root, root, sizeof(ctx->root)) >= sizeof(ctx->root)) { + if (strlen(root) > sizeof(ctx->root) { error_setx(err, "root path too long (max %zu bytes)", sizeof(ctx->root) - 1); goto fail; } + strcpy(ctx->root, root); if (dxcore->initialized) { memset(ctx->nvml_path, 0, strlen(ctx->nvml_path));