CID 1341556: Don't leak map info

This commit is contained in:
christos 2015-12-03 21:30:54 +00:00
parent 0ba3ac345d
commit 0f11011500
6 changed files with 46 additions and 41 deletions

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: create.c,v 1.18 2015/12/03 02:02:43 christos Exp $");
__RCSID("$NetBSD: create.c,v 1.19 2015/12/03 21:30:54 christos Exp $");
#endif
#include <sys/types.h>
@ -102,7 +102,7 @@ create(gpt_t gpt, u_int parts, int force, int primary_only)
mbr->mbr_sig = htole16(MBR_SIG);
gpt_create_pmbr_part(mbr->mbr_part, last);
map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr);
map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1);
if (gpt_write(gpt, map) == -1) {
gpt_warn(gpt, "Can't write PMBR");
return -1;

View File

@ -35,7 +35,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: gpt.c,v 1.60 2015/12/03 02:16:00 christos Exp $");
__RCSID("$NetBSD: gpt.c,v 1.61 2015/12/03 21:30:54 christos Exp $");
#endif
#include <sys/param.h>
@ -308,7 +308,7 @@ gpt_mbr(gpt_t gpt, off_t lba)
(uintmax_t)lba);
else if (gpt->verbose > 1)
gpt_msg(gpt, "PMBR at sector %ju", (uintmax_t)lba);
p = map_add(gpt, lba, 1LL, MAP_TYPE_PMBR, mbr);
p = map_add(gpt, lba, 1LL, MAP_TYPE_PMBR, mbr, 1);
goto out;
}
if (pmbr)
@ -316,7 +316,7 @@ gpt_mbr(gpt_t gpt, off_t lba)
else if (gpt->verbose > 1)
gpt_msg(gpt, "MBR at sector %ju", (uintmax_t)lba);
p = map_add(gpt, lba, 1LL, MAP_TYPE_MBR, mbr);
p = map_add(gpt, lba, 1LL, MAP_TYPE_MBR, mbr, 1);
if (p == NULL)
goto out;
@ -340,8 +340,7 @@ gpt_mbr(gpt_t gpt, off_t lba)
mbr->mbr_part[i].part_typ,
(uintmax_t)start, (uintmax_t)size);
if (mbr->mbr_part[i].part_typ != MBR_PTYPE_EXT_LBA) {
// XXX: map add with non-allocated memory
m = map_add(gpt, start, size, MAP_TYPE_MBR_PART, p);
m = map_add(gpt, start, size, MAP_TYPE_MBR_PART, p, 0);
if (m == NULL)
return -1;
m->map_index = i + 1;
@ -415,13 +414,13 @@ gpt_gpt(gpt_t gpt, off_t lba, int found)
(lba == 1) ? "Pri" : "Sec", (uintmax_t)lba);
m = map_add(gpt, lba, 1, (lba == 1)
? MAP_TYPE_PRI_GPT_HDR : MAP_TYPE_SEC_GPT_HDR, hdr);
? MAP_TYPE_PRI_GPT_HDR : MAP_TYPE_SEC_GPT_HDR, hdr, 1);
if (m == NULL)
return (-1);
m = map_add(gpt, (off_t)le64toh((uint64_t)hdr->hdr_lba_table),
(off_t)blocks,
lba == 1 ? MAP_TYPE_PRI_GPT_TBL : MAP_TYPE_SEC_GPT_TBL, p);
lba == 1 ? MAP_TYPE_PRI_GPT_TBL : MAP_TYPE_SEC_GPT_TBL, p, 1);
if (m == NULL)
return (-1);
@ -444,9 +443,8 @@ gpt_gpt(gpt_t gpt, off_t lba, int found)
(uintmax_t)le64toh(ent->ent_lba_start),
(uintmax_t)size);
}
// XXX: map add with not allocated memory.
m = map_add(gpt, (off_t)le64toh((uint64_t)ent->ent_lba_start),
size, MAP_TYPE_GPT_PART, ent);
size, MAP_TYPE_GPT_PART, ent, 0);
if (m == NULL)
return (-1);
m->map_index = i + 1;
@ -835,7 +833,7 @@ gpt_create(gpt_t gpt, off_t last, u_int parts, int primary_only)
return -1;
}
if ((gpt->gpt = map_add(gpt, 1LL, 1LL,
MAP_TYPE_PRI_GPT_HDR, p)) == NULL) {
MAP_TYPE_PRI_GPT_HDR, p, 1)) == NULL) {
free(p);
gpt_warnx(gpt, "Can't add the primary GPT");
return -1;
@ -846,7 +844,7 @@ gpt_create(gpt_t gpt, off_t last, u_int parts, int primary_only)
return -1;
}
if ((gpt->tbl = map_add(gpt, 2LL, blocks,
MAP_TYPE_PRI_GPT_TBL, p)) == NULL) {
MAP_TYPE_PRI_GPT_TBL, p, 1)) == NULL) {
free(p);
gpt_warnx(gpt, "Can't add the primary GPT table");
return -1;
@ -892,13 +890,13 @@ gpt_create(gpt_t gpt, off_t last, u_int parts, int primary_only)
}
if ((gpt->tpg = map_add(gpt, last, 1LL,
MAP_TYPE_SEC_GPT_HDR, p)) == NULL) {
MAP_TYPE_SEC_GPT_HDR, p, 1)) == NULL) {
gpt_warnx(gpt, "Can't add the secondary GPT");
return -1;
}
if ((gpt->lbt = map_add(gpt, last - blocks, blocks,
MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data)) == NULL) {
MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data, 0)) == NULL) {
gpt_warnx(gpt, "Can't add the secondary GPT table");
return -1;
}

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: map.c,v 1.12 2015/12/02 20:01:44 christos Exp $");
__RCSID("$NetBSD: map.c,v 1.13 2015/12/03 21:30:54 christos Exp $");
#endif
#include <sys/types.h>
@ -46,7 +46,7 @@ __RCSID("$NetBSD: map.c,v 1.12 2015/12/02 20:01:44 christos Exp $");
#include "gpt_private.h"
static map_t
mkmap(off_t start, off_t size, int type)
map_create(off_t start, off_t size, int type)
{
map_t m;
@ -59,9 +59,20 @@ mkmap(off_t start, off_t size, int type)
m->map_type = type;
m->map_index = 0;
m->map_data = NULL;
m->map_alloc = 0;
return m;
}
static void
map_destroy(map_t m)
{
if (m == NULL)
return;
if (m->map_alloc)
free(m->map_data);
free(m);
}
static const char *maptypes[] = {
"unused",
"mbr",
@ -83,7 +94,7 @@ map_type(int t)
}
map_t
map_add(gpt_t gpt, off_t start, off_t size, int type, void *data)
map_add(gpt_t gpt, off_t start, off_t size, int type, void *data, int alloc)
{
map_t m, n, p;
@ -122,6 +133,7 @@ map_add(gpt_t gpt, off_t start, off_t size, int type, void *data)
}
n->map_type = type;
n->map_data = data;
n->map_alloc = alloc;
return n;
}
@ -135,11 +147,12 @@ map_add(gpt_t gpt, off_t start, off_t size, int type, void *data)
n->map_type = MAP_TYPE_UNUSED;
}
m = mkmap(start, size, type);
m = map_create(start, size, type);
if (m == NULL)
goto oomem;
m->map_data = data;
m->map_alloc = alloc;
if (start == n->map_start) {
m->map_prev = n->map_prev;
@ -160,7 +173,7 @@ map_add(gpt_t gpt, off_t start, off_t size, int type, void *data)
p->map_next = m;
p->map_size -= size;
} else {
p = mkmap(n->map_start, start - n->map_start, n->map_type);
p = map_create(n->map_start, start - n->map_start, n->map_type);
if (p == NULL)
goto oomem;
n->map_start += p->map_size + m->map_size;
@ -178,6 +191,7 @@ map_add(gpt_t gpt, off_t start, off_t size, int type, void *data)
return m;
oomem:
map_destroy(m);
gpt_warn(gpt, "Can't create map");
return NULL;
}
@ -221,7 +235,7 @@ map_alloc(gpt_t gpt, off_t start, off_t size, off_t alignment)
size = m->map_size - delta;
}
return map_add(gpt, m->map_start + delta, size,
MAP_TYPE_GPT_PART, NULL);
MAP_TYPE_GPT_PART, NULL, 0);
}
}
@ -257,9 +271,7 @@ map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment)
m->map_next = n->map_next;
if (n->map_next != NULL)
n->map_next->map_prev = m;
if (n->map_data != NULL)
free(n->map_data);
free(n);
map_destroy(n);
return size;
} else { /* alignment > 0 */
prevsize = m->map_size;
@ -277,9 +289,7 @@ map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment)
m->map_next = n->map_next;
if (n->map_next != NULL)
n->map_next->map_prev = m;
if (n->map_data != NULL)
free(n->map_data);
free(n);
map_destroy(n);
}
return size;
}
@ -293,7 +303,7 @@ map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment)
prevsize = m->map_size;
m->map_size = alignsize;
if (n == NULL || n->map_type != MAP_TYPE_UNUSED) {
o = mkmap(m->map_start + alignsize,
o = map_create(m->map_start + alignsize,
prevsize - alignsize, MAP_TYPE_UNUSED);
if (o == NULL) {
gpt_warn(gpt, "Can't create map");
@ -332,9 +342,7 @@ map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment)
m->map_next = n->map_next;
if (n->map_next != NULL)
n->map_next->map_prev = m;
if (n->map_data != NULL)
free(n->map_data);
free(n);
map_destroy(n);
}
m->map_size = alignsize;
return alignsize;
@ -391,7 +399,7 @@ map_init(gpt_t gpt, off_t size)
{
char buf[32];
gpt->mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
gpt->mediamap = map_create(0LL, size, MAP_TYPE_UNUSED);
if (gpt->mediamap == NULL) {
gpt_warn(gpt, "Can't create map");
return -1;

View File

@ -46,11 +46,12 @@ struct map {
#define MAP_TYPE_PMBR 8
unsigned int map_index;
void *map_data;
int map_alloc;
};
struct gpt;
struct map *map_add(struct gpt *, off_t, off_t, int, void *);
struct map *map_add(struct gpt *, off_t, off_t, int, void *, int);
struct map *map_alloc(struct gpt *, off_t, off_t, off_t);
struct map *map_find(struct gpt *, int);
struct map *map_first(struct gpt *);

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: recover.c,v 1.13 2015/12/03 02:02:43 christos Exp $");
__RCSID("$NetBSD: recover.c,v 1.14 2015/12/03 21:30:54 christos Exp $");
#endif
#include <sys/types.h>
@ -98,7 +98,7 @@ recover_gpt_hdr(gpt_t gpt, int type, off_t last)
gpt_warn(gpt, "Cannot allocate %s GPT header", name);
return -1;
}
if ((*dgpt = map_add(gpt, last, 1LL, type, p)) == NULL) {
if ((*dgpt = map_add(gpt, last, 1LL, type, p, 1)) == NULL) {
gpt_warnx(gpt, "Cannot add %s GPT header", name);
return -1;
}
@ -141,8 +141,7 @@ recover_gpt_tbl(gpt_t gpt, int type, off_t start)
return -1;
}
// XXX: non allocated memory
*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data);
*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
if (*dtbl == NULL) {
gpt_warnx(gpt, "Adding %s GPT table failed", name);
return -1;

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: resizedisk.c,v 1.14 2015/12/03 02:09:20 christos Exp $");
__RCSID("$NetBSD: resizedisk.c,v 1.15 2015/12/03 21:30:54 christos Exp $");
#endif
#include <sys/bootblock.h>
@ -180,15 +180,14 @@ resizedisk(gpt_t gpt, off_t sector, off_t size)
return -1;
}
gpt->tpg = map_add(gpt, newloc, 1LL, MAP_TYPE_SEC_GPT_HDR, p);
gpt->tpg = map_add(gpt, newloc, 1LL, MAP_TYPE_SEC_GPT_HDR, p, 1);
if (gpt->tpg == NULL) {
gpt_warn(gpt, "Error adding secondary GPT header");
return -1;
}
// XXX: map add with non-allocated memory
gpt->lbt = map_add(gpt, newloc - gpt_size, gpt_size,
MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data);
MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data, 0);
if (gpt->lbt == NULL) {
gpt_warn(gpt, "Error adding secondary GPT table");
return -1;