Skip to content

Commit a995e6b

Browse files
mhiramatrostedt
authored andcommitted
tools/bootconfig: Fix to check the write failure correctly
Fix to check the write(2) failure including partial write correctly and try to rollback the partial write, because if there is no BOOTCONFIG_MAGIC string, we can not remove it. Link: https://lkml.kernel.org/r/160576521135.320071.3883101436675969998.stgit@devnote2 Fixes: 85c46b7 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly") Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent a61ea63 commit a995e6b

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

‎tools/bootconfig/main.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ static int delete_xbc(const char *path)
337337

338338
static int apply_xbc(const char *path, const char *xbc_path)
339339
{
340+
struct stat stat;
340341
u32 size, csum;
341342
char *buf, *data;
342343
int ret, fd;
@@ -394,23 +395,44 @@ static int apply_xbc(const char *path, const char *xbc_path)
394395
return ret;
395396
}
396397
/* TODO: Ensure the @path is initramfs/initrd image */
398+
if (fstat(fd, &stat) < 0) {
399+
pr_err("Failed to get the size of %s\n", path);
400+
goto out;
401+
}
397402
ret = write(fd, data, size + 8);
398-
if (ret < 0) {
403+
if (ret < size + 8) {
404+
if (ret < 0)
405+
ret = -errno;
399406
pr_err("Failed to apply a boot config: %d\n", ret);
400-
goto out;
407+
if (ret < 0)
408+
goto out;
409+
goto out_rollback;
401410
}
402411
/* Write a magic word of the bootconfig */
403412
ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
404-
if (ret < 0) {
413+
if (ret < BOOTCONFIG_MAGIC_LEN) {
414+
if (ret < 0)
415+
ret = -errno;
405416
pr_err("Failed to apply a boot config magic: %d\n", ret);
406-
goto out;
417+
goto out_rollback;
407418
}
408419
ret = 0;
409420
out:
410421
close(fd);
411422
free(data);
412423

413424
return ret;
425+
426+
out_rollback:
427+
/* Map the partial write to -ENOSPC */
428+
if (ret >= 0)
429+
ret = -ENOSPC;
430+
if (ftruncate(fd, stat.st_size) < 0) {
431+
ret = -errno;
432+
pr_err("Failed to rollback the write error: %d\n", ret);
433+
pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
434+
}
435+
goto out;
414436
}
415437

416438
static int usage(void)

0 commit comments

Comments
 (0)