From 0c19d693bfe1dd3071c71d9d95f68c0db5cc75d0 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 11 Jan 2025 19:39:53 -0600 Subject: Fix genpassword function, delete buggy (v)asprintf implementation(s) --- src/Makefile | 6 +++--- src/encryption.c | 41 ++++++++++++++++++++++++++++------------- src/encryption.h | 2 +- src/shared.c | 41 +---------------------------------------- src/shared.h | 5 ----- 5 files changed, 33 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index a97848d..03f2f05 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CC = gcc SHELL = /usr/bin/bash -DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -Og +DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) $$(pkg-config --cflags libsodium) @@ -14,7 +14,7 @@ RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto LDFLAGS += $(DEBUG_LDFLAGS) $$(pkg-config --libs-only-L libsodium) -BINARIES := main +BINARIES := main encryption .PHONY: all clean @@ -27,5 +27,5 @@ shared.o: shared.c shared.h encryption: encryption.c encryption.h shared.o shared.h -c clean: # huh, didn't think that would work +c clean: rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file diff --git a/src/encryption.c b/src/encryption.c index 0cd032f..052b9aa 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -94,28 +94,38 @@ int encrypttotmp(const char *toencrypt) { } int genpassword(char **str, unsigned int words) { + // Early returns + if(words < 1) + return 0; #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif - unsigned int i = 0; - char *lstr = NULL; - - if(words < 1) - return 0; - - asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); - for(; i < words; i++) { - asprintf(&lstr, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); + // Bootstrap the first word + char *lstr = NULL, *tmp = NULL; + if(asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]) < 0) + return -1; + + // Concat the rest of the words into the password (without leaking memory) + int ret; + for(unsigned int i = 1; i < words; i++) { + ret = asprintf(&tmp, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); + free(lstr); + if(ret < 0) + return -1; + + lstr = tmp; } *str = lstr; + return words; - return 0; - - // TODO: I feel like this is / should be leaking memory like a mofo. Figure out if it is or not (look at malloc_stats()) + // This function was exploding because of some weird conflict with using my buggy implementation of asprintf instead of the + // _GNU_SOURCE version. Don't know why it wasn't using the _GNU_SOURCE version, as I had a define macro put in place to + // prevent it from being compiled if _GNU_SOURCE was defined, but whatever } + #define TESTING #ifdef TESTING @@ -143,9 +153,14 @@ int main(void) { error(1, errno, "close broke"); //*/// + //*// Example code for getting a password using genpassword + checksodium(); + char *password = NULL; genpassword(&password, 20); - printf("%s\n", password); + printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); + free(password); + //*/// return 0; } diff --git a/src/encryption.h b/src/encryption.h index 2c8f976..01aa704 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -4,7 +4,7 @@ // Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May // cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just // manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself -#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1 +#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 0 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 diff --git a/src/shared.c b/src/shared.c index 250b348..b154391 100644 --- a/src/shared.c +++ b/src/shared.c @@ -33,43 +33,4 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) { } return mem; -} - -#if !defined _GNU_SOURCE - -int vasprintf(char **str, const char *format, va_list ap) { - va_list ap2; - int length, ret; - - va_copy(ap2, ap); - if((length = vsnprintf(NULL, 0, format, ap2)) < 0) - return -1; - length++; // + 1 because sprintf does not count the null byte - va_end(ap2); - - char *temp = reallocarray(*str, length, sizeof(char)); - if(temp == NULL) - return -1; - - if((ret = vsnprintf(temp, length, format, ap)) < 0) { - free(temp); - return -1; - } else { - *str = temp; - } - - return ret; -} - -int asprintf(char **str, const char *format, ...) { - va_list ap; - int ret; - - va_start(ap, format); - ret = vasprintf(str, format, ap); - va_end(ap); - - return ret; -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/shared.h b/src/shared.h index 5894b41..620ec82 100644 --- a/src/shared.h +++ b/src/shared.h @@ -17,9 +17,4 @@ void* xcalloc(size_t nmemb, size_t size); // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xreallocarray(void *ptr, size_t nmemb, size_t size); -#if !defined _GNU_SOURCE -int vasprintf(char **str, const char *format, va_list ap); -int asprintf(char **str, const char *format, ...); -#endif - #endif \ No newline at end of file -- cgit v1.2.3