diff --git a/Assemble.c b/Assemble.c index a73e101..c691ec9 100644 --- a/Assemble.c +++ b/Assemble.c @@ -396,6 +396,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd, mdu_array_info_t inf; char *c; char nbuf[64]; + int rc; + if (!st || !st->sb) { return 2; } @@ -408,10 +410,13 @@ int Assemble(struct supertype *st, char *mddev, int mdfd, } if (isdigit(*c) && ((ident->autof & 7)==4 || (ident->autof&7)==6)) /* /dev/md/d0 style for partitionable */ - asprintf(&mddev, "/dev/md/d%s", c); + rc = asprintf(&mddev, "/dev/md/d%s", c); else - asprintf(&mddev, "/dev/md/%s", c); - mdfd = open_mddev(mddev, ident->autof); + rc = asprintf(&mddev, "/dev/md/%s", c); + if (rc < 0) + mdfd = -1; + else + mdfd = open_mddev(mddev, ident->autof); if (mdfd < 0) { st->ss->free_super(st); free(devices); diff --git a/Makefile b/Makefile index 6b878cc..f26eeac 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,9 @@ KLIBC_GCC = gcc -nostdinc -iwithprefix include -I$(KLIBC)/klibc/include -I$(KLIB CC = $(CROSS_COMPILE)gcc CXFLAGS = -ggdb CWFLAGS = -Wall -Werror -Wstrict-prototypes +ifdef WARN_UNUSED +CWFLAGS += -Wp,-D_FORTIFY_SOURCE=2 -O +endif ifdef DEBIAN CPPFLAGS= -DDEBIAN diff --git a/bitmap.c b/bitmap.c index 8617669..0f8a265 100644 --- a/bitmap.c +++ b/bitmap.c @@ -125,7 +125,10 @@ bitmap_info_t *bitmap_fd_read(int fd, int brief) void *buf; int n, skip; - posix_memalign(&buf, 512, 8192); + if (posix_memalign(&buf, 512, 8192) != 0) { + fprintf(stderr, Name ": failed to allocate 8192 bytes\n"); + return NULL; + } n = read(fd, buf, 8192); info = malloc(sizeof(*info)); diff --git a/config.c b/config.c index f471cf3..9aa8be4 100644 --- a/config.c +++ b/config.c @@ -564,10 +564,12 @@ void mailfromline(char *line) if (alert_mail_from == NULL) alert_mail_from = strdup(w); else { - char *t= NULL; - asprintf(&t, "%s %s", alert_mail_from, w); - free(alert_mail_from); - alert_mail_from = t; + char *t = NULL; + + if (asprintf(&t, "%s %s", alert_mail_from, w) > 0) { + free(alert_mail_from); + alert_mail_from = t; + } } } } diff --git a/mdmon.c b/mdmon.c index 568264b..3f5edbb 100644 --- a/mdmon.c +++ b/mdmon.c @@ -85,14 +85,18 @@ int make_pidfile(char *devname, int o_excl) char path[100]; char pid[10]; int fd; + int n; + sprintf(path, "/var/run/mdadm/%s.pid", devname); fd = open(path, O_RDWR|O_CREAT|o_excl, 0600); if (fd < 0) return -errno; sprintf(pid, "%d\n", getpid()); - write(fd, pid, strlen(pid)); + n = write(fd, pid, strlen(pid)); close(fd); + if (n < 0) + return -errno; return 0; } @@ -199,6 +203,7 @@ int main(int argc, char *argv[]) struct sigaction act; int pfd[2]; int status; + int ignore; if (argc != 2) { fprintf(stderr, "Usage: md-manage /device/name/for/container\n"); @@ -218,7 +223,10 @@ int main(int argc, char *argv[]) /* Fork, and have the child tell us when they are ready */ if (do_fork()) { - pipe(pfd); + if (pipe(pfd) != 0) { + fprintf(stderr, "mdmon: failed to create pipe\n"); + exit(1); + } switch(fork()) { case -1: fprintf(stderr, "mdmon: failed to fork: %s\n", @@ -324,18 +332,20 @@ int main(int argc, char *argv[]) /* Ok, this is close enough. We can say goodbye to our parent now. */ status = 0; - write(pfd[1], &status, sizeof(status)); + if (write(pfd[1], &status, sizeof(status)) < 0) + fprintf(stderr, "mdmon: failed to notify our parent: %d\n", + getppid()); close(pfd[1]); - chdir("/"); + ignore = chdir("/"); setsid(); close(0); open("/dev/null", O_RDWR); close(1); - dup(0); + ignore = dup(0); #ifndef DEBUG close(2); - dup(0); + ignore = dup(0); #endif mlockall(MCL_FUTURE); diff --git a/monitor.c b/monitor.c index bd67e1a..1bf69a7 100644 --- a/monitor.c +++ b/monitor.c @@ -486,6 +486,7 @@ static int wait_and_act(struct supertype *container, int nowait) container->ss->sync_metadata(container); } + rv = 0; for (a = *aap; a ; a = a->next) { if (a->replaces && !discard_this) { struct active_array **ap; diff --git a/super-ddf.c b/super-ddf.c index e8f8005..eb9ccbb 100644 --- a/super-ddf.c +++ b/super-ddf.c @@ -504,9 +504,8 @@ static void *load_section(int fd, struct ddf_super *super, void *buf, /* All pre-allocated sections are a single block */ if (len != 1) return NULL; - } else { - posix_memalign(&buf, 512, len<<9); - } + } else if (posix_memalign(&buf, 512, len<<9) != 0) + buf = NULL; if (!buf) return NULL; @@ -642,9 +641,13 @@ static int load_ddf_local(int fd, struct ddf_super *super, unsigned long long dsize; /* First the local disk info */ - posix_memalign((void**)&dl, 512, + if (posix_memalign((void**)&dl, 512, sizeof(*dl) + - (super->max_part) * sizeof(dl->vlist[0])); + (super->max_part) * sizeof(dl->vlist[0])) != 0) { + fprintf(stderr, Name ": %s could not allocate disk info buffer\n", + __func__); + return 1; + } load_section(fd, super, &dl->disk, super->active->data_section_offset, @@ -693,8 +696,14 @@ static int load_ddf_local(int fd, struct ddf_super *super, if (vd->magic == DDF_SPARE_ASSIGN_MAGIC) { if (dl->spare) continue; - posix_memalign((void**)&dl->spare, 512, - super->conf_rec_len*512); + if (posix_memalign((void**)&dl->spare, 512, + super->conf_rec_len*512) != 0) { + fprintf(stderr, Name + ": %s could not allocate spare info buf\n", + __func__); + return 1; + } + memcpy(dl->spare, vd, super->conf_rec_len*512); continue; } @@ -712,9 +721,14 @@ static int load_ddf_local(int fd, struct ddf_super *super, __be32_to_cpu(vcl->conf.seqnum)) continue; } else { - posix_memalign((void**)&vcl, 512, + if (posix_memalign((void**)&vcl, 512, (super->conf_rec_len*512 + - offsetof(struct vcl, conf))); + offsetof(struct vcl, conf))) != 0) { + fprintf(stderr, Name + ": %s could not allocate vcl buf\n", + __func__); + return 1; + } vcl->next = super->conflist; vcl->block_sizes = NULL; /* FIXME not for CONCAT */ super->conflist = vcl; @@ -804,7 +818,16 @@ static int load_super_ddf(struct supertype *st, int fd, return rv; } - load_ddf_local(fd, super, devname, 0); + rv = load_ddf_local(fd, super, devname, 0); + + if (rv) { + if (devname) + fprintf(stderr, + Name ": Failed to load all information " + "sections on %s\n", devname); + free(super); + return rv; + } /* Should possibly check the sections .... */ @@ -1473,7 +1496,10 @@ static int init_super_ddf(struct supertype *st, return init_super_ddf_bvd(st, info, size, name, homehost, uuid); - posix_memalign((void**)&ddf, 512, sizeof(*ddf)); + if (posix_memalign((void**)&ddf, 512, sizeof(*ddf)) != 0) { + fprintf(stderr, Name ": %s could not allocate superblock\n", __func__); + return 0; + } memset(ddf, 0, sizeof(*ddf)); ddf->dlist = NULL; /* no physical disks yet */ ddf->conflist = NULL; /* No virtual disks yet */ @@ -1600,7 +1626,10 @@ static int init_super_ddf(struct supertype *st, memset(ddf->controller.pad, 0xff, 8); memset(ddf->controller.vendor_data, 0xff, 448); - posix_memalign((void**)&pd, 512, pdsize); + if (posix_memalign((void**)&pd, 512, pdsize) != 0) { + fprintf(stderr, Name ": %s could not allocate pd\n", __func__); + return 0; + } ddf->phys = pd; ddf->pdsize = pdsize; @@ -1611,7 +1640,10 @@ static int init_super_ddf(struct supertype *st, pd->max_pdes = __cpu_to_be16(max_phys_disks); memset(pd->pad, 0xff, 52); - posix_memalign((void**)&vd, 512, vdsize); + if (posix_memalign((void**)&vd, 512, vdsize) != 0) { + fprintf(stderr, Name ": %s could not allocate vd\n", __func__); + return 0; + } ddf->virt = vd; ddf->vdsize = vdsize; memset(vd, 0, vdsize); @@ -1839,8 +1871,11 @@ static int init_super_ddf_bvd(struct supertype *st, __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1); /* Now create a new vd_config */ - posix_memalign((void**)&vcl, 512, - (offsetof(struct vcl, conf) + ddf->conf_rec_len * 512)); + if (posix_memalign((void**)&vcl, 512, + (offsetof(struct vcl, conf) + ddf->conf_rec_len * 512)) != 0) { + fprintf(stderr, Name ": %s could not allocate vd_config\n", __func__); + return 0; + } vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[ddf->mppe]; vcl->vcnum = venum; sprintf(st->subarray, "%d", venum); @@ -2010,8 +2045,13 @@ static void add_to_super_ddf(struct supertype *st, * a phys_disk entry and a more detailed disk_data entry. */ fstat(fd, &stb); - posix_memalign((void**)&dd, 512, - sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part); + if (posix_memalign((void**)&dd, 512, + sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part) != 0) { + fprintf(stderr, Name + ": %s could allocate buffer for new disk, aborting\n", + __func__); + abort(); + } dd->major = major(stb.st_rdev); dd->minor = minor(stb.st_rdev); dd->devname = devname; @@ -2547,13 +2587,18 @@ static int load_super_ddf_all(struct supertype *st, int fd, close(dfd); /* Now we need the device-local bits */ for (sd = sra->devs ; sd ; sd = sd->next) { + int rv; + sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY); if (dfd < 0) return 2; - load_ddf_headers(dfd, super, NULL); - seq = load_ddf_local(dfd, super, NULL, keep_fd); + rv = load_ddf_headers(dfd, super, NULL); + if (rv == 0) + rv = load_ddf_local(dfd, super, NULL, keep_fd); if (!keep_fd) close(dfd); + if (rv) + return 1; } if (st->subarray[0]) { struct vcl *v; @@ -2679,16 +2724,20 @@ static int store_zero_ddf(struct supertype *st, int fd) { unsigned long long dsize; void *buf; + int rc; if (!get_dev_size(fd, NULL, &dsize)) return 1; - posix_memalign(&buf, 512, 512); + if (posix_memalign(&buf, 512, 512) != 0) + return 1; memset(buf, 0, 512); lseek64(fd, dsize-512, 0); - write(fd, buf, 512); + rc = write(fd, buf, 512); free(buf); + if (rc < 0) + return 1; return 0; } diff --git a/super0.c b/super0.c index 24ea009..291ccdd 100644 --- a/super0.c +++ b/super0.c @@ -546,7 +546,10 @@ static int init_super0(struct supertype *st, mdu_array_info_t *info, mdp_super_t *sb; int spares; - posix_memalign((void**)&sb, 512, MD_SB_BYTES + sizeof(bitmap_super_t)); + if (posix_memalign((void**)&sb, 512, MD_SB_BYTES + sizeof(bitmap_super_t)) != 0) { + fprintf(stderr, Name ": %s could not allocate superblock\n", __func__); + return 0; + } memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t)); st->sb = sb; @@ -739,8 +742,12 @@ static int compare_super0(struct supertype *st, struct supertype *tst) if (second->md_magic != MD_SB_MAGIC) return 1; if (!first) { - posix_memalign((void**)&first, 512, - MD_SB_BYTES + sizeof(struct bitmap_super_s)); + if (posix_memalign((void**)&first, 512, + MD_SB_BYTES + sizeof(struct bitmap_super_s)) != 0) { + fprintf(stderr, Name + ": %s could not allocate superblock\n", __func__); + return 1; + } memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s)); st->sb = first; return 0; @@ -809,7 +816,12 @@ static int load_super0(struct supertype *st, int fd, char *devname) return 1; } - posix_memalign((void**)&super, 512, MD_SB_BYTES + sizeof(bitmap_super_t)+512); + if (posix_memalign((void**)&super, 512, + MD_SB_BYTES + sizeof(bitmap_super_t)+512) != 0) { + fprintf(stderr, Name + ": %s could not allocate superblock\n", __func__); + return 1; + } if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) { if (devname) diff --git a/super1.c b/super1.c index b7cd962..a392cf6 100644 --- a/super1.c +++ b/super1.c @@ -689,8 +689,12 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info, int rfd; char defname[10]; - posix_memalign((void**)&sb, 512, (1024 + 512 + - sizeof(struct misc_dev_info))); + if (posix_memalign((void**)&sb, 512, (1024 + 512 + + sizeof(struct misc_dev_info))) != 0) { + fprintf(stderr, Name + ": %s could not allocate superblock\n", __func__); + return 0; + } memset(sb, 0, 1024); st->sb = sb; @@ -1036,9 +1040,13 @@ static int compare_super1(struct supertype *st, struct supertype *tst) return 1; if (!first) { - posix_memalign((void**)&first, 512, + if (posix_memalign((void**)&first, 512, 1024 + 512 + - sizeof(struct misc_dev_info)); + sizeof(struct misc_dev_info)) != 0) { + fprintf(stderr, Name + ": %s could not allocate superblock\n", __func__); + return 1; + } memcpy(first, second, 1024 + 512 + sizeof(struct misc_dev_info)); st->sb = first; @@ -1152,9 +1160,13 @@ static int load_super1(struct supertype *st, int fd, char *devname) return 1; } - posix_memalign((void**)&super, 512, + if (posix_memalign((void**)&super, 512, 1024 + 512 + - sizeof(struct misc_dev_info)); + sizeof(struct misc_dev_info)) != 0) { + fprintf(stderr, Name ": %s could not allocate superblock\n", + __func__); + return 1; + } if (read(fd, super, 1024) != 1024) { if (devname)