From 77c02889177c43df8ebe07bce4d55b7f7358118f Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Tue, 16 Nov 2021 20:49:23 +0000 Subject: [PATCH] fstools: fix a couple of minor code problems Improve error handling and fix a resource leak inside an error path. Signed-off-by: Daniel Golle --- libblkid-tiny/libblkid-tiny.c | 18 +++++++++++++----- libfstools/common.c | 12 +++++++++--- libfstools/mount.c | 7 +++++-- libfstools/overlay.c | 10 ++++++++-- libfstools/partname.c | 3 +++ libfstools/snapshot.c | 21 ++++++++++++++------- 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/libblkid-tiny/libblkid-tiny.c b/libblkid-tiny/libblkid-tiny.c index 6e8cd81..8520f8a 100644 --- a/libblkid-tiny/libblkid-tiny.c +++ b/libblkid-tiny/libblkid-tiny.c @@ -87,11 +87,15 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr, memset(bf, 0, sizeof(*bf)); bf->data = ((unsigned char *)bf) + sizeof(*bf); - lseek(pr->fd, off, SEEK_SET); + if (lseek(pr->fd, off, SEEK_SET) < 0) { + fprintf(stderr, "failed to seek\n"); + free(bf); + return NULL; + } ret = read(pr->fd, bf->data, len); if (ret != len) { - fprintf(stderr, "faile to read blkid\n"); + fprintf(stderr, "failed to read blkid\n"); free(bf); return NULL; } @@ -212,10 +216,14 @@ int probe_block(char *block, struct blkid_struct_probe *pr) int off = (mag->kboff * 1024) + mag->sboff; char magic[32] = { 0 }; - lseek(pr->fd, off, SEEK_SET); - if (read(pr->fd, magic, mag->len) < 0) + if (lseek(pr->fd, off, SEEK_SET) < 0) { + close(pr->fd); return -1; - + } + if (read(pr->fd, magic, mag->len) < 0) { + close(pr->fd); + return -1; + } DEBUG("magic: %s %s %d\n", mag->magic, magic, mag->len); if (!memcmp(mag->magic, magic, mag->len)) break; diff --git a/libfstools/common.c b/libfstools/common.c index 8dc1095..1b22d2c 100644 --- a/libfstools/common.c +++ b/libfstools/common.c @@ -59,13 +59,17 @@ int block_file_identify(FILE *f, uint64_t offset) uint32_t magic = 0; size_t n; - fseeko(f, offset, SEEK_SET); + if (fseeko(f, offset, SEEK_SET) < 0) + return -1; + n = fread(&magic, sizeof(magic), 1, f); if (magic == cpu_to_le32(0x88b1f)) { return FS_TARGZ; } - fseeko(f, offset + 0x400, SEEK_SET); + if (fseeko(f, offset + 0x400, SEEK_SET) < 0) + return -1; + n = fread(&magic, sizeof(magic), 1, f); if (n != 1) return -1; @@ -74,7 +78,9 @@ int block_file_identify(FILE *f, uint64_t offset) return FS_F2FS; magic = 0; - fseeko(f, offset + 0x438, SEEK_SET); + if (fseeko(f, offset + 0x438, SEEK_SET) < 0) + return -1; + n = fread(&magic, sizeof(magic), 1, f); if (n != 1) return -1; diff --git a/libfstools/mount.c b/libfstools/mount.c index 1691ce7..3d4111f 100644 --- a/libfstools/mount.c +++ b/libfstools/mount.c @@ -143,8 +143,11 @@ fopivot(char *rw_root, char *ro_root) * We can't really deal with these constraints without * creating two new subdirectories in /overlay. */ - mkdir(upperdir, 0755); - mkdir(workdir, 0755); + if (mkdir(upperdir, 0755) == -1 && errno != EEXIST) + return -1; + + if (mkdir(workdir, 0755) == -1 && errno != EEXIST) + return -1; if (stat(upgrade, &st) == 0) rename(upgrade, upgrade_dest); diff --git a/libfstools/overlay.c b/libfstools/overlay.c index d7d8f48..5de12c5 100644 --- a/libfstools/overlay.c +++ b/libfstools/overlay.c @@ -175,14 +175,20 @@ static int switch2jffs(struct volume *v) { struct stat s; - int ret; + int ret, fd; if (!stat(SWITCH_JFFS2, &s)) { ULOG_ERR("jffs2 switch already running\n"); return -1; } - creat(SWITCH_JFFS2, 0600); + fd = creat(SWITCH_JFFS2, 0600); + if (fd == -1) { + ULOG_ERR("failed - cannot create jffs2 switch mark: %m\n"); + return -1; + } + close(fd); + ret = mount(v->blk, OVERLAYDIR, "jffs2", MS_NOATIME, NULL); unlink(SWITCH_JFFS2); if (ret) { diff --git a/libfstools/partname.c b/libfstools/partname.c index 773df8c..f59c52e 100644 --- a/libfstools/partname.c +++ b/libfstools/partname.c @@ -152,6 +152,9 @@ static struct volume *partname_volume_find(char *name) devname = gl.gl_pathv[j]; tmp = strrchr(devname, '/'); + if (!tmp) + return NULL; + *tmp = '\0'; devname = strrchr(devname, '/') + 1; diff --git a/libfstools/snapshot.c b/libfstools/snapshot.c index 4870cf7..5e7e847 100644 --- a/libfstools/snapshot.c +++ b/libfstools/snapshot.c @@ -145,7 +145,7 @@ snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint3 } in = open(file, O_RDONLY); - if (in < 1) { + if (in < 0) { ULOG_ERR("failed to open %s\n", file); goto out; } @@ -161,7 +161,7 @@ snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint3 ret = 0; out: - if (in > 0) + if (in >= 0) close(in); return ret; @@ -190,7 +190,7 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type) return -1; out = open(file, O_WRONLY | O_CREAT, 0700); - if (!out) { + if (out < 0) { ULOG_ERR("failed to open %s\n", file); return -1; } @@ -203,10 +203,14 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type) if (hdr.length < len) len = hdr.length; - if (volume_read(v, buffer, offset, len)) + if (volume_read(v, buffer, offset, len)) { + close(out); return -1; - if (write(out, buffer, len) != len) + } + if (write(out, buffer, len) != len) { + close(out); return -1; + } offset += len; hdr.length -= len; } @@ -219,7 +223,7 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type) return 0; } - block += pad_file_size(v, hdr.length) / v->block_size; + block += pad_file_size(v, hdr.length) / v->block_size; return block; } @@ -334,7 +338,10 @@ mount_snapshot(struct volume *v) return -1; } foreachdir("/overlay/", handle_whiteout); - mkdir("/volatile", 0700); + if (mkdir("/volatile", 0700) == -1 && errno != EEXIST) { + perror("mkdir"); + return -1; + } _ramoverlay("/rom", "/volatile"); mount_move("/rom/volatile", "/volatile", ""); mount_move("/rom/rom", "/rom", ""); -- 2.30.2