zkey: Protect from symlink-following attacks

Files in the zkey repository can be created by any member of the
'zkeyadm' group as well as 'root'. Such files are owned by the creator
and the 'zkeyadm' group, and allow read and write for the owner user
and owner group.

When creating or writing files inside the zkey repository, make sure
that the file is not a sysmlink. That way, only files within the zkey
repository are set to be read/write for the owner user and members of
the 'zkeyadm' group. Make sure to open such files with the 'O_NOFOLLOW'
flag, and use 'lstat()' to check for files and directories.

Assisted-by: IBM Bob:2.0.0
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Reviewed-by: Finn Callies <fcallies@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Ingo Franzki
2026-06-30 09:46:27 +02:00
committed by Jan Höppner
parent 278f4f6fd5
commit 94292dac54
7 changed files with 104 additions and 32 deletions
+31 -2
View File
@@ -1225,7 +1225,7 @@ int copy_file(const char *in_file_name, const char *out_file_name,
goto out;
}
fp_out = fopen(out_file_name, "w");
fp_out = fopen_nofollow(out_file_name, "w");
if (fp_out == NULL) {
rc = -errno;
warnx("Failed to open '%s': %s", out_file_name, strerror(-rc));
@@ -1366,7 +1366,7 @@ int store_passphrase_from_base64(const char *b64_string, const char *filename,
return -ENOMEM;
}
fp = fopen(filename, "w");
fp = fopen_nofollow(filename, "w");
if (fp == NULL) {
pr_verbose(verbose, "Open of file '%s' failed: %s", filename,
strerror(errno));
@@ -1393,3 +1393,32 @@ out:
return rc;
}
FILE *fopen_nofollow(const char *path, const char *mode)
{
int flags = O_NOFOLLOW;
int fd;
FILE *fp;
/* Determine flags based on mode */
if (mode[0] == 'r')
flags |= (mode[1] == '+') ? O_RDWR : O_RDONLY;
else if (mode[0] == 'w')
flags |= O_CREAT | O_TRUNC |
((mode[1] == '+') ? O_RDWR : O_WRONLY);
else if (mode[0] == 'a')
flags |= O_CREAT | O_APPEND |
((mode[1] == '+') ? O_RDWR : O_WRONLY);
else
return NULL;
fd = open(path, flags, 0600);
if (fd < 0)
return NULL;
fp = fdopen(fd, mode);
if (fp == NULL) {
close(fd);
return NULL;
}
return fp;
}