From 09c01e580abc519976c8e20c5d867b3d1a31e062 Mon Sep 17 00:00:00 2001 From: Vineeth Vijayan Date: Wed, 7 Jun 2023 14:10:57 +0200 Subject: [PATCH] zdev: use rename-file to avoid any symlinks created MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit During the boot, the ZDEV_SITE_ID is derived with the help of loadparm and will be saved in ZDEV_SITE_ID_FILE, which will be the used by the udev-rules. ZDEV_SITE_ID_FILE creation can have a surface of symlink attack as we are directly using the fopen and fprintf on it. To avoid this, make sure that we are writing the ZDEV_SITE_ID to a temporary file, which will then be renamed to ZDEV_SITE_ID_FILE, which will remove all the existing symlinks associated with the target file. Reported-by: Marc Hartmayer Signed-off-by: Vineeth Vijayan Reviewed-by: Peter Oberparleiter Signed-off-by: Jan Höppner --- zdev/src/zdev_id.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/zdev/src/zdev_id.c b/zdev/src/zdev_id.c index 9ad99618..2464b166 100644 --- a/zdev/src/zdev_id.c +++ b/zdev/src/zdev_id.c @@ -213,9 +213,16 @@ out: static void write_zdev_site_id(int site_id) { FILE *fd; - int rc; + int tmpfd, rc; + const char zdev_id_file[] = ZDEV_SITE_ID_FILE; + char zdev_id_tmpfile[] = ZDEV_SITE_ID_FILE "-XXXXXX"; - fd = fopen(ZDEV_SITE_ID_FILE, "w"); + tmpfd = mkstemp(zdev_id_tmpfile); + if (tmpfd == -1) + goto err; + + /* Open the temp file to use with fprintf */ + fd = fdopen(tmpfd, "w"); if (!fd) goto err; @@ -232,6 +239,12 @@ static void write_zdev_site_id(int site_id) if (fclose(fd)) goto err; + /* Rename the temporary file to ZDEV_SITE_ID_FILE*/ + if (rename(zdev_id_tmpfile, zdev_id_file) == -1) { + remove(zdev_id_tmpfile); + goto err; + } + return; err: err(1, "Could not write to zdev_site_id file");