general: add memory auditing

memory_utils: added to wrap memory alloc / free
general: adjusted stabdard and other memory allocs t be audited
This commit is contained in:
Ian-Clowes 2020-08-03 04:01:58 +01:00 committed by Polynomialdivision
parent 421324486f
commit d56c5c4e15
14 changed files with 355 additions and 93 deletions

View file

@ -50,6 +50,7 @@ int compare_essid_iwinfo(struct dawn_mac bssid_addr, struct dawn_mac bssid_addr_
iw = iwinfo_backend(entry->d_name);
// TODO: Magic number
static char buf_bssid[18] = {0};
if (iw->bssid(entry->d_name, buf_bssid))
snprintf(buf_bssid, sizeof(buf_bssid), "00:00:00:00:00:00");

View file

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "memory_utils.h"
#include "datastorage.h"
#include "dawn_iwinfo.h"
#include "dawn_uci.h"
@ -23,6 +24,7 @@ void uci_get_hostname(char* hostname)
char path[]= "system.@system[0].hostname";
struct uci_ptr ptr;
struct uci_context *c = uci_alloc_context();
dawn_regmem(c);
if(!c){
return;
@ -30,6 +32,7 @@ void uci_get_hostname(char* hostname)
if ((uci_lookup_ptr(c, &ptr, path, true) != UCI_OK) || (ptr.o==NULL || ptr.o->v.string==NULL)){
uci_free_context(c);
dawn_unregmem(c);
return;
}
@ -46,6 +49,7 @@ void uci_get_hostname(char* hostname)
}
uci_free_context(c);
dawn_unregmem(c);
}
struct time_config_s uci_get_time_config() {
@ -197,6 +201,7 @@ int uci_init() {
if (!ctx) {
ctx = uci_alloc_context();
dawn_regmem(ctx);
uci_ctx = ctx;
ctx->flags &= ~UCI_FLAG_STRICT;
@ -209,7 +214,11 @@ int uci_init() {
}
if (uci_load(ctx, "dawn", &uci_pkg))
{
// TODO: Is this allocating memory?
dawn_regmem(uci_pkg);
return -1;
}
return 1;
}
@ -217,9 +226,11 @@ int uci_init() {
int uci_clear() {
if (uci_pkg != NULL) {
uci_unload(uci_ctx, uci_pkg);
dawn_unregmem(uci_pkg);
}
if (uci_ctx != NULL) {
uci_free_context(uci_ctx);
dawn_unregmem(uci_ctx);
}
return 1;
}
@ -232,6 +243,7 @@ int uci_set_network(char* uci_cmd)
if (!ctx) {
ctx = uci_alloc_context();
dawn_regmem(ctx);
uci_ctx = ctx;
}

View file

@ -50,6 +50,8 @@ void write_mac_to_file(char* path, struct dawn_mac addr) {
FILE* f = fopen(path, "a");
if (f == NULL) {
fprintf(stderr, "Error opening mac file!\n");
// TODO: Should this be an exit()?
exit(1);
}

172
src/utils/memory_utils.c Normal file
View file

@ -0,0 +1,172 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "memory_utils.h"
#define DAWN_MEM_FILENAME_LEN 20
struct mem_list {
struct mem_list* next_mem;
int line;
char file[DAWN_MEM_FILENAME_LEN];
char type;
size_t size;
void* ptr;
};
struct mem_list* mem_base = NULL;
void* dawn_memory_alloc(enum dawn_memop type, char* file, int line, size_t nmemb, size_t size, void *ptr)
{
void* ret = NULL;
switch (type)
{
case DAWN_MALLOC:
ret = malloc(size);
break;
case DAWN_REALLOC:
dawn_memory_unregister(DAWN_REALLOC, file, line, ptr);
ret = realloc(ptr, size);
break;
case DAWN_CALLOC:
ret = calloc(nmemb, size);
size *= nmemb; // May not be correct allowing for padding but gives a sense of scale
break;
default:
break;
}
if (ret != NULL)
dawn_memory_register(type, file, line, size, ret);
return ret;
}
void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t size, void* ptr)
{
void* ret = NULL;
struct mem_list* this_log = NULL;
char type_c = '?';
// Ignore over enthusiastic effort to register a failed allocation
if (ptr == NULL)
return ret;
switch (type)
{
case DAWN_MALLOC:
type_c = 'M';
break;
case DAWN_REALLOC:
type_c = 'R';
break;
case DAWN_CALLOC:
type_c = 'C';
break;
case DAWN_MEMREG:
type_c = 'X';
break;
default:
printf("Unexpected memory op tag!\n");
break;
}
// Insert to linked list with ascending memory reference
struct mem_list** ipos = &mem_base;
while (*ipos != NULL && (*ipos)->ptr < ptr)
ipos = &((*ipos)->next_mem);
if (*ipos != NULL && (*ipos)->ptr == ptr)
{
printf("attempting to register memory already registered (%c@%s:%d)...\n", type_c, file, line);
}
else
{
this_log = malloc(sizeof(struct mem_list));
if (this_log == NULL)
{
printf("Oh the irony! malloc() failed in dawn_memory_register()!\n");
}
else
{
this_log->next_mem = *ipos;
*ipos = this_log;
// Just use filename - no path
file = strrchr(file, '/');
if (file != NULL)
strncpy(this_log->file, file + 1, DAWN_MEM_FILENAME_LEN);
else
strncpy(this_log->file, "?? UNKNOWN ??", DAWN_MEM_FILENAME_LEN);
this_log->type = type_c;
this_log->line = line;
this_log->ptr = ptr;
this_log->size = size;
}
}
return ret;
}
void dawn_memory_unregister(enum dawn_memop type, char* file, int line, void* ptr)
{
struct mem_list** mem = &mem_base;
char type_c = '?';
while (*mem != NULL && (*mem)->ptr <= ptr)
{
mem = &((*mem)->next_mem);
}
switch (type)
{
case DAWN_FREE:
type_c = 'F';
break;
case DAWN_MEMUNREG:
type_c = 'U';
break;
case DAWN_REALLOC:
type_c = 'R';
break;
default:
printf("Unexpected memory op tag!\n");
break;
}
if (*mem != NULL && (*mem)->ptr == ptr)
{
struct mem_list* tmp = *mem;
*mem = tmp->next_mem;
free(tmp);
}
else
{
printf("Releasing (%c) memory we hadn't registered (%s:%d)...\n", type_c, file, line);
}
return;
}
void dawn_memory_free(enum dawn_memop type, char* file, int line, void* ptr)
{
dawn_memory_unregister(type, file, line, ptr);
free(ptr);
return;
}
void dawn_memory_audit()
{
printf("Currently recorded memory allocations...\n");
for (struct mem_list* mem = mem_base; mem != NULL; mem = mem->next_mem)
{
printf("%c - %s@%d: %ld\n", mem->type, mem->file, mem->line, mem->size);
}
printf("[End of memory allocation list]\n");
}

View file

@ -1,5 +1,6 @@
#include <libubus.h>
#include "memory_utils.h"
#include "dawn_uci.h"
#include "datastorage.h"
#include "ubus.h"
@ -146,10 +147,10 @@ int parse_to_hostapd_notify(struct blob_attr* msg, hostapd_notify_entry* notify_
probe_entry *parse_to_probe_req(struct blob_attr* msg) {
struct blob_attr* tb[__PROB_MAX];
probe_entry* prob_req = malloc(sizeof(probe_entry));
probe_entry* prob_req = dawn_malloc(sizeof(probe_entry));
if (prob_req == NULL)
{
fprintf(stderr, "malloc of probe_entry failed!\n");
fprintf(stderr, "dawn_malloc of probe_entry failed!\n");
return NULL;
}
@ -157,19 +158,19 @@ probe_entry *parse_to_probe_req(struct blob_attr* msg) {
if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr.u8))
{
free(prob_req);
dawn_free(prob_req);
return NULL;
}
if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr.u8))
{
free(prob_req);
dawn_free(prob_req);
return NULL;
}
if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr.u8))
{
free(prob_req);
dawn_free(prob_req);
return NULL;
}
@ -286,7 +287,7 @@ int handle_network_msg(char* msg) {
if (entry != insert_to_array(entry, false, false, false)) // use 802.11k values
{
// insert found an existing entry, rather than linking in our new one
free(entry);
dawn_free(entry);
}
}
}
@ -358,7 +359,7 @@ dump_rrm_table(struct blob_attr* head, int len) //modify from examples/blobmsg-e
static void
dump_client(struct blob_attr** tb, struct dawn_mac client_addr, const char* bssid_addr, uint32_t freq, uint8_t ht_supported,
uint8_t vht_supported) {
client *client_entry = malloc(sizeof(struct client_s));
client *client_entry = dawn_malloc(sizeof(struct client_s));
if (client_entry == NULL)
{
// MUSTDO: Error handling?
@ -480,7 +481,7 @@ int parse_to_clients(struct blob_attr* msg, int do_kick, uint32_t id) {
num_stations = dump_client_table(blobmsg_data(tb[CLIENT_TABLE]), blobmsg_data_len(tb[CLIENT_TABLE]),
blobmsg_data(tb[CLIENT_TABLE_BSSID]), blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]),
blobmsg_get_u8(tb[CLIENT_TABLE_HT]), blobmsg_get_u8(tb[CLIENT_TABLE_VHT]));
ap *ap_entry = malloc(sizeof(struct ap_s));
ap *ap_entry = dawn_malloc(sizeof(struct ap_s));
hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), ap_entry->bssid_addr.u8);
ap_entry->freq = blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]);

View file

@ -1,6 +1,7 @@
#include <dirent.h>
#include <libubus.h>
#include "memory_utils.h"
#include "networksocket.h"
#include "tcpsocket.h"
#include "mac_utils.h"
@ -349,10 +350,10 @@ int parse_to_beacon_rep(struct blob_attr *msg) {
{
printf("Beacon: No Probe Entry Existing!\n");
probe_entry* beacon_rep = malloc(sizeof(probe_entry));
probe_entry* beacon_rep = dawn_malloc(sizeof(probe_entry));
if (beacon_rep == NULL)
{
fprintf(stderr, "malloc of probe_entry failed!\n");
fprintf(stderr, "dawn_malloc of probe_entry failed!\n");
return -1;
}
@ -376,7 +377,7 @@ int parse_to_beacon_rep(struct blob_attr *msg) {
{
// insert found an existing entry, rather than linking in our new one
ubus_send_probe_via_network(beacon_rep);
free(beacon_rep);
dawn_free(beacon_rep);
}
else
ubus_send_probe_via_network(beacon_rep);
@ -389,7 +390,7 @@ int ret = WLAN_STATUS_SUCCESS;
bool discard_entry = true;
print_probe_array();
auth_entry *auth_req = malloc(sizeof(struct auth_entry_s));
auth_entry *auth_req = dawn_malloc(sizeof(struct auth_entry_s));
if (auth_req == NULL)
return -1;
@ -426,7 +427,7 @@ bool discard_entry = true;
}
if (discard_entry)
free(auth_req);
dawn_free(auth_req);
return ret;
}
@ -436,7 +437,7 @@ int ret = WLAN_STATUS_SUCCESS;
int discard_entry = true;
print_probe_array();
auth_entry* auth_req = malloc(sizeof(struct auth_entry_s));
auth_entry* auth_req = dawn_malloc(sizeof(struct auth_entry_s));
if (auth_req == NULL)
return -1;
@ -472,13 +473,13 @@ int discard_entry = true;
}
if (discard_entry)
free(auth_req);
dawn_free(auth_req);
return ret;
}
static int handle_probe_req(struct blob_attr *msg) {
// MUSTDO: Untangle malloc() and linking of probe_entry
// MUSTDO: Untangle dawn_malloc() and linking of probe_entry
probe_entry* probe_req = parse_to_probe_req(msg);
if (probe_req != NULL) {
@ -487,7 +488,7 @@ static int handle_probe_req(struct blob_attr *msg) {
{
// insert found an existing entry, rather than linking in our new one
ubus_send_probe_via_network(probe_req);
free(probe_req);
dawn_free(probe_req);
}
else
ubus_send_probe_via_network(probe_req);
@ -499,7 +500,7 @@ static int handle_probe_req(struct blob_attr *msg) {
}
}
// TODO: Retrun for malloc() failure?
// TODO: Retrun for dawn_malloc() failure?
return WLAN_STATUS_SUCCESS;
}
@ -514,7 +515,7 @@ static int handle_beacon_rep(struct blob_attr *msg) {
}
int send_blob_attr_via_network(struct blob_attr *msg, char *method) {
int send_blob_attr_via_network(struct blob_attr* msg, char* method) {
if (!msg) {
return -1;
@ -523,11 +524,13 @@ int send_blob_attr_via_network(struct blob_attr *msg, char *method) {
char *data_str;
char *str;
data_str = blobmsg_format_json(msg, true);
dawn_regmem(data_str);
blob_buf_init(&b_send_network, 0);
blobmsg_add_string(&b_send_network, "method", method);
blobmsg_add_string(&b_send_network, "data", data_str);
str = blobmsg_format_json(b_send_network.head, true);
dawn_regmem(str);
if (network_config.network_option == 2) {
send_tcp(str);
@ -539,8 +542,8 @@ int send_blob_attr_via_network(struct blob_attr *msg, char *method) {
}
}
free(data_str);
free(str);
dawn_free(data_str);
dawn_free(str);
return 0;
}
@ -550,8 +553,9 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg) {
char *str;
str = blobmsg_format_json(msg, true);
dawn_regmem(str);
printf("Method new: %s : %s\n", method, str);
free(str);
dawn_free(str);
struct hostapd_sock_entry *entry;
struct ubus_subscriber *subscriber;
@ -593,6 +597,7 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) {
return -1;
} else {
printf("Connected to ubus\n");
dawn_regmem(ctx);
}
ubus_add_uloop(ctx);
@ -630,6 +635,7 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) {
close_socket();
ubus_free(ctx);
dawn_unregmem(ctx);
uloop_done();
return 0;
}
@ -641,6 +647,7 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_
return;
char *data_str = blobmsg_format_json(msg, 1);
dawn_regmem(data_str);
blob_buf_init(&b_domain, 0);
blobmsg_add_json_from_string(&b_domain, data_str);
blobmsg_add_u32(&b_domain, "collision_domain", network_config.collision_domain);
@ -655,13 +662,13 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_
if (entry == NULL) {
fprintf(stderr, "Failed to find interface!\n");
free(data_str);
dawn_free(data_str);
return;
}
if (!entry->subscribed) {
fprintf(stderr, "Interface %s is not subscribed!\n", entry->iface_name);
free(data_str);
dawn_free(data_str);
return;
}
@ -687,7 +694,7 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_
print_client_array();
print_ap_array();
free(data_str);
dawn_free(data_str);
}
static int ubus_get_clients() {
@ -1248,7 +1255,7 @@ bool subscriber_to_interface(const char *ifname) {
struct hostapd_sock_entry *hostapd_entry;
hostapd_entry = calloc(1, sizeof(struct hostapd_sock_entry));
hostapd_entry = dawn_calloc(1, sizeof(struct hostapd_sock_entry));
strcpy(hostapd_entry->iface_name, ifname);
// add hostname