2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-22 18:17:09 +00:00

Subject: parser - use DUP_STRING more widely and detect strdup errors

This patch moves the DUP_STRING macro to parser.h and modifies
it to accept a goto error target, that will be jumped to if the
call to strdup(3) fails. It also uses it in additional locations
where copying structures occurs, as well as detecting additional
cases where a structure duplication might have failed but not been
propagated outward.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Tyler Hicks <tyhicks@canonical.com>
This commit is contained in:
Steve Beattie 2013-09-06 13:39:41 -07:00
parent ebabb30abd
commit dd5145131e
4 changed files with 36 additions and 16 deletions

View File

@ -141,9 +141,6 @@ out:
return ent; return ent;
} }
#define DUP_STRING(orig, new, field) \
(new)->field = (orig)->field ? strdup((orig)->field) : NULL
struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig) struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
{ {
struct dbus_entry *ent = NULL; struct dbus_entry *ent = NULL;
@ -151,12 +148,12 @@ struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
if (!ent) if (!ent)
return NULL; return NULL;
DUP_STRING(orig, ent, bus); DUP_STRING(orig, ent, bus, err);
DUP_STRING(orig, ent, name); DUP_STRING(orig, ent, name, err);
DUP_STRING(orig, ent, peer_label); DUP_STRING(orig, ent, peer_label, err);
DUP_STRING(orig, ent, path); DUP_STRING(orig, ent, path, err);
DUP_STRING(orig, ent, interface); DUP_STRING(orig, ent, interface, err);
DUP_STRING(orig, ent, member); DUP_STRING(orig, ent, member, err);
ent->mode = orig->mode; ent->mode = orig->mode;
ent->audit = orig->audit; ent->audit = orig->audit;
ent->deny = orig->deny; ent->deny = orig->deny;
@ -164,6 +161,10 @@ struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
ent->next = orig->next; ent->next = orig->next;
return ent; return ent;
err:
free_dbus_entry(ent);
return NULL;
} }
void print_dbus_entry(struct dbus_entry *ent) void print_dbus_entry(struct dbus_entry *ent)

View File

@ -466,7 +466,6 @@ void free_mnt_entry(struct mnt_entry *ent)
free(ent); free(ent);
} }
struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig) struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
{ {
struct mnt_entry *entry = NULL; struct mnt_entry *entry = NULL;
@ -475,12 +474,17 @@ struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
if (!entry) if (!entry)
return NULL; return NULL;
entry->mnt_point = orig->mnt_point ? strdup(orig->mnt_point) : NULL; DUP_STRING(orig, entry, mnt_point, err);
entry->device = orig->device ? strdup(orig->device) : NULL; DUP_STRING(orig, entry, device, err);
entry->trans = orig->trans ? strdup(orig->trans) : NULL; DUP_STRING(orig, entry, trans, err);
entry->dev_type = dup_value_list(orig->dev_type); entry->dev_type = dup_value_list(orig->dev_type);
if (orig->dev_type && !(entry->dev_type))
goto err;
entry->opts = dup_value_list(orig->opts); entry->opts = dup_value_list(orig->opts);
if (orig->opts && !(entry->opts))
goto err;
entry->flags = orig->flags; entry->flags = orig->flags;
entry->inv_flags = orig->inv_flags; entry->inv_flags = orig->inv_flags;
@ -492,6 +496,10 @@ struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
entry->next = orig->next; entry->next = orig->next;
return entry; return entry;
err:
free_mnt_entry(entry);
return NULL;
} }
void print_mnt_entry(struct mnt_entry *entry) void print_mnt_entry(struct mnt_entry *entry)

View File

@ -261,6 +261,13 @@ extern int preprocess_only;
___tmp->next = (LISTB); \ ___tmp->next = (LISTB); \
} while (0) } while (0)
#define DUP_STRING(orig, new, field, fail_target) \
do { \
(new)->field = ((orig)->field) ? strdup((orig)->field) : NULL; \
if (((orig)->field) && !((new)->field)) \
goto fail_target; \
} while (0)
/* from parser_common.c */ /* from parser_common.c */
extern int regex_type; extern int regex_type;
extern int perms_create; extern int perms_create;

View File

@ -840,9 +840,9 @@ struct cod_entry *copy_cod_entry(struct cod_entry *orig)
if (!entry) if (!entry)
return NULL; return NULL;
entry->namespace = orig->namespace ? strdup(orig->namespace) : NULL; DUP_STRING(orig, entry, namespace, err);
entry->name = strdup(orig->name); DUP_STRING(orig, entry, name, err);
entry->link_name = orig->link_name ? strdup(orig->link_name) : NULL; DUP_STRING(orig, entry, link_name, err);
entry->mode = orig->mode; entry->mode = orig->mode;
entry->audit = orig->audit; entry->audit = orig->audit;
entry->deny = orig->deny; entry->deny = orig->deny;
@ -854,6 +854,10 @@ struct cod_entry *copy_cod_entry(struct cod_entry *orig)
entry->next = orig->next; entry->next = orig->next;
return entry; return entry;
err:
free_cod_entries(entry);
return NULL;
} }
void free_cod_entries(struct cod_entry *list) void free_cod_entries(struct cod_entry *list)