diff options
author | axtloss <axtlos@getcryst.al> | 2024-07-11 02:56:56 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-07-11 02:56:56 +0200 |
commit | 3e85fcb0270c9224ab95dac02c737e6676974c8e (patch) | |
tree | 2ec96323ef26f9ec1fee8ac6a0b718700e909eda /src | |
parent | e8f6d6c71b45062cc7ec4dcadcecba44af39a15d (diff) | |
download | extlib-3e85fcb0270c9224ab95dac02c737e6676974c8e.tar.gz extlib-3e85fcb0270c9224ab95dac02c737e6676974c8e.tar.bz2 |
Implement memset_s and improve free_secure
Diffstat (limited to 'src')
-rw-r--r-- | src/constraint_handler.c | 66 | ||||
-rw-r--r-- | src/extlib.c | 7 | ||||
-rw-r--r-- | src/extlib.h | 20 | ||||
-rw-r--r-- | src/memset_s.c | 48 |
4 files changed, 138 insertions, 3 deletions
diff --git a/src/constraint_handler.c b/src/constraint_handler.c new file mode 100644 index 0000000..ca4e9d2 --- /dev/null +++ b/src/constraint_handler.c @@ -0,0 +1,66 @@ +/* constraint_handler.c + * + * Copyright 2024 axtlos <axtlos@disroot.org> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * SPDX-License-Identifier: LGPL-3.0-only + */ + + +#define __STDC_WANT_LIB_EXT1__ 1 +#include "extlib.h" + +static constraint_handler_t *_cnstrnt_handler = NULL; + +constraint_handler_t +set_constraint_handler_s (constraint_handler_t handler) +{ + constraint_handler_t *new, *old, ret; + + new = malloc (sizeof (constraint_handler_t)); + if (!new) return NULL; + *new = handler; + old = _cnstrnt_handler; + _cnstrnt_handler = new; + if (!old) + ret = NULL; + else { + ret = *old; + free (old); + } + return ret; +} + +void +throw_constraint_handler_s (const char * restrict msg, errno_t error) +{ + constraint_handler_t ch; + ch = _cnstrnt_handler != NULL ? *_cnstrnt_handler : NULL; + if (ch != NULL) + ch (msg, NULL, error); +} + +void +abort_handler_s (const char *restrict msg, void * restrict ptr, errno_t error) +{ + static const char abrt_msg[] = "abort_handler: "; + + fprintf (stderr, "%s", abrt_msg); + fprintf (stderr, "%s\n", msg); + abort (); +} + +void +ignore_handler_s (const char * restrict msg, void * restrict ptr, errno_t error) +{} diff --git a/src/extlib.c b/src/extlib.c index 4fa7213..107e8b4 100644 --- a/src/extlib.c +++ b/src/extlib.c @@ -20,13 +20,14 @@ #define _XOPEN_SOURCE 500 #define USE_SECURE_MEM +#define __STDC_WANT_LIB_EXT1__ 1 #include <ftw.h> #include "extlib.h" #include <stdlib.h> #include <stdio.h> #include <string.h> - +#include <time.h> #undef free #undef malloc @@ -34,7 +35,7 @@ void free_secure(void **__ptr, size_t ptrlen) { - memset (*__ptr, 0, ptrlen); + memset_s (*__ptr, ptrlen+1, 0, ptrlen); free (*__ptr); *__ptr = NULL; return; @@ -43,7 +44,7 @@ free_secure(void **__ptr, size_t ptrlen) void * malloc_secure (size_t len) { - void *mem = calloc (0, len); + void *mem = calloc (1, len); return mem; } diff --git a/src/extlib.h b/src/extlib.h index 87948f5..fc7a3d2 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -20,6 +20,7 @@ #include <stdlib.h> #include <stdio.h> #include <unistd.h> +#include <stdint.h> #ifdef USE_SECURE_MEM #define free(x) error - use free_secure @@ -32,6 +33,25 @@ void free_secure (void **__ptr, size_t ptrlen); void *malloc_secure (size_t len); #endif +#if (__STDC_WANT_LIB_EXT1__ == 1) +#define RSIZE_MAX SIZE_MAX +typedef int errno_t; +typedef size_t rsize_t; + +typedef void (*constraint_handler_t) (const char *__restrict, void *__restrict, errno_t); +/// Copy the value of c (converted to an unsigned char) into each of the first n +/// characters of the object pointed to by s. +/// Conforms to ISO/IEC 9899:2011 K.3.7.4.1 +errno_t memset_s (void *s, rsize_t max, int c, rsize_t n); + +constraint_handler_t set_constraint_handler_s (constraint_handler_t handler); +void throw_constraint_handler_s (const char *restrict msg, errno_t error); +_Noreturn void abort_handler_s (const char * __restrict, void * __restrict, + errno_t); +void ignore_handler_s (const char *__restrict, void *__restrict, errno_t); +#endif + + /// Copy the data of one filestream to another */ void fcopy(FILE *f1, FILE *f2); diff --git a/src/memset_s.c b/src/memset_s.c new file mode 100644 index 0000000..deda5d7 --- /dev/null +++ b/src/memset_s.c @@ -0,0 +1,48 @@ +/* memset_s.c + * + * Copyright 2024 axtlos <axtlos@disroot.org> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * SPDX-License-Identifier: LGPL-3.0-only + */ + + +#define __STDC_WANT_LIB_EXT1__ 1 +#include "extlib.h" +#include <errno.h> + + +errno_t +memset_s(void *s, rsize_t smax, int c, rsize_t n) +{ + volatile unsigned char *dest = (unsigned char *) s; + errno_t ret = EINVAL; + rsize_t limit = n < smax ? n : smax; + + if (!s) + throw_constraint_handler_s("memset_s: s = NULL", ret); + else if (n > RSIZE_MAX) + throw_constraint_handler_s("memset_s: n > RSIZE_MAX", ret); + else if (smax > RSIZE_MAX) + throw_constraint_handler_s("memset_s: smax > RSIZE_MAX", ret); + else if (n > smax) + throw_constraint_handler_s("memset_s: n > smax", ret); + else { + while (limit > 0) + dest[--limit] = (unsigned char)c; + ret = 0; + } + return ret; +} + |