Skip to content

Commit 8cdee08

Browse files
committed
prepare-root: Properly check return value of snprintf()
When the target buffer is to small to hold the resulting string a value larger or equal than the buffer's size is returned. (In pre C99 versions, snprintf returned -1 in this case, too.) So to ensure that no truncated paths are used adapt the error checking accordingly.
1 parent 0660e83 commit 8cdee08

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/switchroot/ostree-prepare-root-static.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ resolve_deploy_path (const char *root_mountpoint)
121121
errx (EXIT_FAILURE, "Failed to read kernel cmdline");
122122
autofree char *ostree_cmdline = find_proc_cmdline_key (kernel_cmdline, "ostree");
123123

124-
if (snprintf (destpath, sizeof (destpath), "%s/%s", root_mountpoint, ostree_cmdline) < 0)
124+
int ret = snprintf (destpath, sizeof (destpath), "%s/%s", root_mountpoint, ostree_cmdline);
125+
if (ret < 0 || ret >= sizeof (destpath))
125126
err (EXIT_FAILURE, "failed to assemble ostree target path");
126127
if (lstat (destpath, &stbuf) < 0)
127128
err (EXIT_FAILURE, "Couldn't find specified OSTree root '%s'", destpath);
@@ -238,13 +239,15 @@ main (int argc, char *argv[])
238239
/* Prepare /boot.
239240
* If /boot is on the same partition, use a bind mount to make it visible
240241
* at /boot inside the deployment. */
241-
if (snprintf (srcpath, sizeof (srcpath), "%s/boot/loader", root_mountpoint) < 0)
242+
int ret = snprintf (srcpath, sizeof (srcpath), "%s/boot/loader", root_mountpoint);
243+
if (ret < 0 || ret >= sizeof (srcpath))
242244
err (EXIT_FAILURE, "failed to assemble /boot/loader path");
243245
if (lstat (srcpath, &stbuf) == 0 && S_ISLNK (stbuf.st_mode))
244246
{
245247
if (lstat ("boot", &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
246248
{
247-
if (snprintf (srcpath, sizeof (srcpath), "%s/boot", root_mountpoint) < 0)
249+
ret = snprintf (srcpath, sizeof (srcpath), "%s/boot", root_mountpoint);
250+
if (ret < 0 || ret >= sizeof (srcpath))
248251
err (EXIT_FAILURE, "failed to assemble /boot path");
249252
if (mount (srcpath, TMP_SYSROOT "/boot", NULL, MS_BIND | MS_SILENT, NULL) < 0)
250253
err (EXIT_FAILURE, "failed to bind mount %s to boot", srcpath);

0 commit comments

Comments
 (0)