Using mg_bzero to cleanup memory in mg_close_conn()

This commit is contained in:
cpq committed 2023-09-17 22:25:36 +01:00
1 parent a04c94444e
commit 7ecbbc517d
6 files changed
+293 -289

No files matched your search

+5 -12
View File
@@ -1,14 +1,7 @@
#include "iobuf.h"
#include "arch.h"
#include "iobuf.h"
#include "log.h"
// Not using memset for zeroing memory, cause it can be dropped by compiler
// See https://github.com/cesanta/mongoose/pull/1265
static void zeromem(volatile unsigned char *buf, size_t len) {
if (buf != NULL) {
while (len--) *buf++ = 0;
}
}
#include "util.h"
static size_t roundup(size_t size, size_t align) {
return align == 0 ? size : (size + align - 1) / align * align;
@@ -18,7 +11,7 @@ int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) {
int ok = 1;
new_size = roundup(new_size, io->align);
if (new_size == 0) {
zeromem(io->buf, io->size);
mg_bzero(io->buf, io->size);
free(io->buf);
io->buf = NULL;
io->len = io->size = 0;
@@ -29,7 +22,7 @@ int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) {
if (p != NULL) {
size_t len = new_size < io->len ? new_size : io->len;
if (len > 0 && io->buf != NULL) memmove(p, io->buf, len);
zeromem(io->buf, io->size);
mg_bzero(io->buf, io->size);
free(io->buf);
io->buf = (unsigned char *) p;
io->size = new_size;
@@ -64,7 +57,7 @@ size_t mg_iobuf_del(struct mg_iobuf *io, size_t ofs, size_t len) {
if (ofs > io->len) ofs = io->len;
if (ofs + len > io->len) len = io->len - ofs;
if (io->buf) memmove(io->buf + ofs, io->buf + ofs + len, io->len - ofs - len);
if (io->buf) zeromem(io->buf + io->len - len, len);
if (io->buf) mg_bzero(io->buf + io->len - len, len);
io->len -= len;
return len;
}
+1 -1
View File
@@ -143,7 +143,7 @@ void mg_close_conn(struct mg_connection *c) {
mg_tls_free(c);
mg_iobuf_free(&c->recv);
mg_iobuf_free(&c->send);
memset(c, 0, sizeof(*c));
mg_bzero((unsigned char *) c, sizeof(*c));
free(c);
}
+9 -1
View File
@@ -1,5 +1,13 @@
#include "util.h"
// Not using memset for zeroing memory, cause it can be dropped by compiler
// See https://github.com/cesanta/mongoose/pull/1265
void mg_bzero(volatile unsigned char *buf, size_t len) {
if (buf != NULL) {
while (len--) *buf++ = 0;
}
}
#if MG_ENABLE_CUSTOM_RANDOM
#else
void mg_random(void *buf, size_t len) {
@@ -86,7 +94,7 @@ int mg_check_ip_acl(struct mg_str acl, struct mg_addr *remote_ip) {
uint32_t remote_ip4;
if (remote_ip->is_ip6) {
return -1; // TODO(): handle IPv6 ACL and addresses
} else { // IPv4
} else { // IPv4
memcpy((void *) &remote_ip4, remote_ip->ip, sizeof(remote_ip4));
while (mg_commalist(&acl, &k, &v)) {
uint32_t net, mask;
+1
View File
@@ -11,6 +11,7 @@
#define assert(x)
#endif
void mg_bzero(volatile unsigned char *buf, size_t len);
void mg_random(void *buf, size_t len);
char *mg_random_str(char *buf, size_t len);
uint16_t mg_ntohs(uint16_t net);