Skip to content

Commit

Permalink
add checking of return status on fstat calls
Browse files Browse the repository at this point in the history
There are a few places we don't check the return status when
calling fstat for success. Clean up the calls by adding a
check before continuing.

Signed-off-by: Nigel Croxon <[email protected]>
  • Loading branch information
ncroxon authored and mtkaczyk committed May 21, 2024
1 parent c587986 commit 95673c7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
41 changes: 23 additions & 18 deletions Assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,9 @@ static int load_devices(struct devs *devices, char *devmap,
/* prepare useful information in info structures */
struct stat stb2;
int err;
fstat(mdfd, &stb2);

if (fstat(mdfd, &stb2) != 0)
goto error;

if (c->update == UOPT_UUID && !ident->uuid_set)
random_uuid((__u8 *)ident->uuid);
Expand All @@ -675,13 +677,10 @@ static int load_devices(struct devs *devices, char *devmap,
devname);
if (dfd >= 0)
close(dfd);
close(mdfd);
free(devices);
free(devmap);
tst->ss->free_super(tst);
free(tst);
*stp = st;
return -1;
goto error;
}
tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);

Expand Down Expand Up @@ -715,12 +714,9 @@ static int load_devices(struct devs *devices, char *devmap,
map_num(update_options, c->update), tst->ss->name);
tst->ss->free_super(tst);
free(tst);
close(mdfd);
close(dfd);
free(devices);
free(devmap);
*stp = st;
return -1;
goto error;
}
if (c->update == UOPT_UUID &&
!ident->uuid_set) {
Expand Down Expand Up @@ -751,18 +747,23 @@ static int load_devices(struct devs *devices, char *devmap,
devname);
if (dfd >= 0)
close(dfd);
close(mdfd);
free(devices);
free(devmap);
tst->ss->free_super(tst);
free(tst);
*stp = st;
return -1;
goto error;
}
tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
}

fstat(dfd, &stb);
if (fstat(dfd, &stb) != 0) {
close(dfd);
free(devices);
free(devmap);
tst->ss->free_super(tst);
free(tst);
*stp = st;
return -1;
}
close(dfd);

if (c->verbose > 0)
Expand Down Expand Up @@ -842,12 +843,9 @@ static int load_devices(struct devs *devices, char *devmap,
inargv ? "the list" :
"the\n DEVICE list in mdadm.conf"
);
close(mdfd);
free(devices);
free(devmap);
free(best);
*stp = st;
return -1;
goto error;
}
if (best[i] == -1 || (devices[best[i]].i.events
< devices[devcnt].i.events))
Expand All @@ -863,6 +861,13 @@ static int load_devices(struct devs *devices, char *devmap,
*bestp = best;
*stp = st;
return devcnt;

error:
close(mdfd);
free(devices);
free(devmap);
return -1;

}

static int force_array(struct mdinfo *content,
Expand Down
11 changes: 9 additions & 2 deletions Dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int Dump_metadata(char *dev, char *dir, struct context *c,
unsigned long long size;
DIR *dirp;
struct dirent *de;
int ret = 0;

if (stat(dir, &stb) != 0 ||
(S_IFMT & stb.st_mode) != S_IFDIR) {
Expand Down Expand Up @@ -112,9 +113,15 @@ int Dump_metadata(char *dev, char *dir, struct context *c,
}
if (c->verbose >= 0)
printf("%s saved as %s.\n", dev, fname);
fstat(fd, &dstb);
close(fd);

close(fl);
ret = fstat(fd, &dstb);
close(fd);
if (ret) {
unlink(fname);
free(fname);
return 1;
}
if ((dstb.st_mode & S_IFMT) != S_IFBLK) {
/* Not a block device, so cannot create links */
free(fname);
Expand Down
12 changes: 8 additions & 4 deletions Grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,13 +1223,14 @@ int reshape_open_backup_file(char *backup_file,
* way this will not notice, but it is better than
* nothing.
*/
fstat(*fdlist, &stb);
if (fstat(*fdlist, &stb) != 0)
goto error;
dev = stb.st_dev;
fstat(fd, &stb);
if (fstat(fd, &stb) != 0)
goto error;
if (stb.st_rdev == dev) {
pr_err("backup file must NOT be on the array being reshaped.\n");
close(*fdlist);
return 0;
goto error;
}

memset(buf, 0, 512);
Expand All @@ -1255,6 +1256,9 @@ int reshape_open_backup_file(char *backup_file,
}

return 1;
error:
close(*fdlist);
return 0;
}

unsigned long compute_backup_blocks(int nchunk, int ochunk,
Expand Down
3 changes: 2 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,8 @@ void conf_file_or_dir(FILE *f)
struct dirent *dp;
struct fname *list = NULL;

fstat(fileno(f), &st);
if (fstat(fileno(f), &st) != 0)
return;
if (S_ISREG(st.st_mode))
conf_file(f);
else if (!S_ISDIR(st.st_mode))
Expand Down
3 changes: 2 additions & 1 deletion mdstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ void mdstat_wait_fd(int fd, const sigset_t *sigmask)

if (fd >= 0) {
struct stat stb;
fstat(fd, &stb);
if (fstat(fd, &stb) != 0)
return;
if ((stb.st_mode & S_IFMT) == S_IFREG)
/* Must be a /proc or /sys fd, so expect
* POLLPRI
Expand Down
8 changes: 6 additions & 2 deletions super-ddf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,10 @@ static int load_ddf_local(int fd, struct ddf_super *super,
0);
dl->devname = devname ? xstrdup(devname) : NULL;

fstat(fd, &stb);
if (fstat(fd, &stb) != 0) {
free(dl);
return 1;
}
dl->major = major(stb.st_rdev);
dl->minor = minor(stb.st_rdev);
dl->next = super->dlist;
Expand Down Expand Up @@ -2786,7 +2789,8 @@ static int add_to_super_ddf(struct supertype *st,
/* This is device numbered dk->number. We need to create
* a phys_disk entry and a more detailed disk_data entry.
*/
fstat(fd, &stb);
if (fstat(fd, &stb) != 0)
return 1;
n = find_unused_pde(ddf);
if (n == DDF_NOTFOUND) {
pr_err("No free slot in array, cannot add disk\n");
Expand Down
8 changes: 6 additions & 2 deletions super-intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -4239,7 +4239,10 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)

dl = xcalloc(1, sizeof(*dl));

fstat(fd, &stb);
if (fstat(fd, &stb) != 0) {
free(dl);
return 1;
}
dl->major = major(stb.st_rdev);
dl->minor = minor(stb.st_rdev);
dl->next = super->disks;
Expand Down Expand Up @@ -5981,7 +5984,8 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
if (super->current_vol >= 0)
return add_to_super_imsm_volume(st, dk, fd, devname);

fstat(fd, &stb);
if (fstat(fd, &stb) != 0)
return 1;
dd = xcalloc(sizeof(*dd), 1);
dd->major = major(stb.st_rdev);
dd->minor = minor(stb.st_rdev);
Expand Down

0 comments on commit 95673c7

Please sign in to comment.