Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/gpt CID 1341556: Don't leak map info
details: https://anonhg.NetBSD.org/src/rev/b98dd749634c
branches: trunk
changeset: 342043:b98dd749634c
user: christos <christos%NetBSD.org@localhost>
date: Thu Dec 03 21:30:54 2015 +0000
description:
CID 1341556: Don't leak map info
diffstat:
sbin/gpt/create.c | 4 ++--
sbin/gpt/gpt.c | 24 +++++++++++-------------
sbin/gpt/map.c | 42 +++++++++++++++++++++++++-----------------
sbin/gpt/map.h | 3 ++-
sbin/gpt/recover.c | 7 +++----
sbin/gpt/resizedisk.c | 7 +++----
6 files changed, 46 insertions(+), 41 deletions(-)
diffs (truncated from 350 to 300 lines):
diff -r cddea672de9e -r b98dd749634c sbin/gpt/create.c
--- a/sbin/gpt/create.c Thu Dec 03 21:04:59 2015 +0000
+++ b/sbin/gpt/create.c Thu Dec 03 21:30:54 2015 +0000
@@ -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 @@
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;
diff -r cddea672de9e -r b98dd749634c sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c Thu Dec 03 21:04:59 2015 +0000
+++ b/sbin/gpt/gpt.c Thu Dec 03 21:30:54 2015 +0000
@@ -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 @@
(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 @@
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 @@
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 @@
(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 @@
(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 @@
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 @@
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 @@
}
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;
}
diff -r cddea672de9e -r b98dd749634c sbin/gpt/map.c
--- a/sbin/gpt/map.c Thu Dec 03 21:04:59 2015 +0000
+++ b/sbin/gpt/map.c Thu Dec 03 21:30:54 2015 +0000
@@ -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 @@
#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 @@
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_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 @@
}
n->map_type = type;
n->map_data = data;
+ n->map_alloc = alloc;
return n;
}
@@ -135,11 +147,12 @@
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 @@
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 @@
return m;
oomem:
+ map_destroy(m);
gpt_warn(gpt, "Can't create map");
return NULL;
}
@@ -221,7 +235,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
{
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;
diff -r cddea672de9e -r b98dd749634c sbin/gpt/map.h
--- a/sbin/gpt/map.h Thu Dec 03 21:04:59 2015 +0000
+++ b/sbin/gpt/map.h Thu Dec 03 21:30:54 2015 +0000
@@ -46,11 +46,12 @@
#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 *);
diff -r cddea672de9e -r b98dd749634c sbin/gpt/recover.c
--- a/sbin/gpt/recover.c Thu Dec 03 21:04:59 2015 +0000
+++ b/sbin/gpt/recover.c Thu Dec 03 21:30:54 2015 +0000
@@ -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
Home |
Main Index |
Thread Index |
Old Index