Skip to content

Commit cb6ec6c

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 cb6ec6c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ 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)
125126
err (EXIT_FAILURE, "failed to assemble ostree target path");
127+
if (ret >= sizeof (destpath))
128+
errx (EXIT_FAILURE, "path too long while assembling ostree target path");
126129
if (lstat (destpath, &stbuf) < 0)
127130
err (EXIT_FAILURE, "Couldn't find specified OSTree root '%s'", destpath);
128131
if (!S_ISLNK (stbuf.st_mode))
@@ -238,14 +241,22 @@ main (int argc, char *argv[])
238241
/* Prepare /boot.
239242
* If /boot is on the same partition, use a bind mount to make it visible
240243
* at /boot inside the deployment. */
241-
if (snprintf (srcpath, sizeof (srcpath), "%s/boot/loader", root_mountpoint) < 0)
244+
int ret = snprintf (srcpath, sizeof (srcpath), "%s/boot/loader", root_mountpoint);
245+
if (ret < 0)
242246
err (EXIT_FAILURE, "failed to assemble /boot/loader path");
247+
if (ret >= sizeof (srcpath))
248+
errx (EXIT_FAILURE, "path too long while assembling /boot/loader path");
243249
if (lstat (srcpath, &stbuf) == 0 && S_ISLNK (stbuf.st_mode))
244250
{
245251
if (lstat ("boot", &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
246252
{
247-
if (snprintf (srcpath, sizeof (srcpath), "%s/boot", root_mountpoint) < 0)
253+
ret = snprintf (srcpath, sizeof (srcpath), "%s/boot", root_mountpoint);
254+
if (ret < 0)
248255
err (EXIT_FAILURE, "failed to assemble /boot path");
256+
/*
257+
* ret >= sizeof (srcpath) cannot happen here because then writing
258+
* "${root_mountpoint}/boot/loader" above would have failed already.
259+
*/
249260
if (mount (srcpath, TMP_SYSROOT "/boot", NULL, MS_BIND | MS_SILENT, NULL) < 0)
250261
err (EXIT_FAILURE, "failed to bind mount %s to boot", srcpath);
251262
}

0 commit comments

Comments
 (0)