From 27fd79fa995506819d8cabff9dd7303d8ec8bbaa Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 26 Oct 2025 01:39:56 -0500 Subject: Fix encryption suite after ripping out cleanup function suite --- src/encryption.c | 152 ++++++++++++++++++------------------------------------- src/shared.c | 8 +-- 2 files changed, 52 insertions(+), 108 deletions(-) (limited to 'src') diff --git a/src/encryption.c b/src/encryption.c index 2ead9cf..8526f15 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -226,17 +226,6 @@ int linkto(const char * const target, int tgfd) { } -static void __ucl_close(void *fd) { - if(!fd) return; - close(*(int*)fd); - return; -} - -static void __ucl_fclose(void *file) { - if(!file) return; - fclose((FILE*)file); - return; -} /** * @brief Encrypt src to dst using libsodium's xchacha encryption suite @@ -369,71 +358,45 @@ int encryptviatmp(const char * const target, const char * const output, const un checksodium(); #endif - cleanup_CREATE(10); - int fd = -1, tfd = -1; + int fd = -1, tfd = -1, res = -1; FILE *src, *dst; char *targetdir; // Open the target file - if((fd = open(target, O_RDONLY)) < 0) - return -1; - cleanup_REGISTER(__ucl_close, (void*)&fd); + if((fd = open(target, O_RDONLY)) < 0) return -1; // Create a temp file for writing targetdir = vxdirname(output); - if(!targetdir) - cleanup_MARK(); - cleanup_CNDREGISTER(free, targetdir); + if(!targetdir) goto CLEANUP_encryptviatmp; // Actually get the file descriptor for the temp file - cleanup_CNDEXEC( - tfd = maketmp(targetdir); - if(tfd < 0) - cleanup_MARK(); - cleanup_CNDREGISTER(__ucl_close, (void*)&tfd); - ); + tfd = maketmp(targetdir); + if(tfd < 0) goto CLEANUP_encryptviatmp; // Create a FILE* version of the source fd - cleanup_CNDEXEC( - if(!(src = fdopen(fd, "rb"))) { - WARN(errno, " Couldn't open \"%s\"", , target); - cleanup_MARK(); - } - cleanup_CNDREGISTER(__ucl_fclose, (void*)&src); - ) + if(!(src = fdopen(fd, "rb"))) goto CLEANUP_encryptviatmp; // Create a FILE* version of the target fd - cleanup_CNDEXEC( - if(!(dst = fdopen(tfd, "wb"))) { - WARN(errno, " Couldn't open \"%s\"", , output); - cleanup_MARK(); - } - cleanup_CNDREGISTER(__ucl_fclose, (void*)dst); - ); - + if(!(dst = fdopen(tfd, "wb"))) goto CLEANUP_encryptviatmp; // Do the encryption now that everything has been set up - cleanup_CNDEXEC( - // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't - if(encrypttofile(src, dst, key) < 0) - ERROR(1, ENOTRECOVERABLE, " I don't even have a way to cause an error here. How did you do it?",); + if(encrypttofile(src, dst, key) < 0) // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't + ERROR(1, ENOTRECOVERABLE, " I don't even have a way to cause an error here. How did you do it?",); - // Link the temp file into the system - if(linkto(output, tfd) < 0) - WARN(errno, " Could not link \"%s\" into system after encryption", , output); + // Link the temp file into the system + if(linkto(output, tfd) < 0) + WARN(errno, " Could not link \"%s\" into system after encryption", , output); - free(targetdir); - fclose(dst); - fclose(src); - // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors - ); + res = 0; +CLEANUP_encryptviatmp: + free(targetdir); + fclose(src); + fclose(dst); + close(fd); + close(tfd); - cleanup_CNDFIRE(); - if(cleanup_ERRORFLAGGED) - return -1; - - return 0; + return res; } /** @@ -450,61 +413,43 @@ int decryptto(const char * const target, const char * const output, const unsign checksodium(); #endif - cleanup_CREATE(10); FILE *src, *dst; - int fdst; + int fdst, eflag = -1, res = -1; // Open the source file - if(!(src = fopen(target, "rb"))) { - WARN(errno, " Could not open \"%s\" for decryption", , target); - return -1; - } - cleanup_REGISTER(__ucl_fclose, src); + if(!(src = fopen(target, "rb"))) {eflag = 0; goto CLEANUP_decryptto;} // Get a temp descriptor for the temp file - cleanup_CNDEXEC( - fdst = maketmp(output); - if(!fdst) { - WARN(errno, " Could not get temp file for decryption", ); - cleanup_MARK(); - } - cleanup_CNDREGISTER(__ucl_close, (void*)&fdst); - ); + if(!(fdst = maketmp(output))) {eflag = 1; goto CLEANUP_decryptto;} // Open a FILE* version of the temp file - cleanup_CNDEXEC( - if(!(dst = fdopen(fdst, "wb"))) { - WARN(errno, " Could not open \"%s\" for writing decrypted data", , output); - cleanup_MARK(); - } - cleanup_CNDREGISTER(__ucl_fclose, dst); - ); + if(!(dst = fdopen(fdst, "wb"))) {eflag = 2; goto CLEANUP_decryptto;} // Follow through with the rest of the decryption - cleanup_CNDEXEC( - if(decrypttofile(src, dst, key) < 0) - ERROR(1, errno, " How did you even cause an error?",); - - // Link temp into system - if(linkto(output, fdst) < 0) - WARN(errno, " Could not link \"%s\" into system", , output); - - fclose(dst); - fclose(src); - // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors - ); - - cleanup_CNDFIRE(); - if(cleanup_ERRORFLAGGED) - return -1; - - // Note: If an error were to theoretically occur, which shouldn't be possible but I'm covering my bases here, after the - // `dst = fdopen` line, a double close on the temp file descriptor would occur. I've been told that this is not catastrophic, - // and considering how my multithreading works it *should* be fine, but it very well could cause problems. The easy solution is - // to man up and just write another cleanup function to pop the last thing off the stack, but again this is an error I can't - // actually trigger so it's fine for now + if(decrypttofile(src, dst, key) < 0) {eflag = 3; goto CLEANUP_decryptto;} + + // Link temp into system + if(linkto(output, fdst) < 0) {eflag = 4; goto CLEANUP_decryptto;} + + res = 0; + +CLEANUP_decryptto: + fclose(src); + fclose(dst); + close(fdst); + + if(___VXGG___VERBOSE_ERRORS___) { + switch (eflag) { + case 0: WARN(errno, " Could not open \"%s\" for decryption", , target); break; + case 1: WARN(errno, " Could not get temp file for decryption", ); break; + case 2: WARN(errno, " Could not open \"%s\" for writing decrypted data", , output); break; + case 3: ERROR(1, errno, " How did you even cause an error?",); break; + case 4: WARN(errno, " Could not link \"%s\" into system", , output); break; + default: WARN(errno, " Ran into an error", ); break; + } + } - return 0; + return res; } /** @@ -618,11 +563,10 @@ ctqueue * cryptscan() { ctqueue *res = ctqueue_init(TPSIZE), *working = ctqueue_init(TPSIZE); if(!res || !working) ERRRET(errno, NULL); - task *start = task_init(__cscan_worker, free, NULL); + task *start = task_new(__cscan_worker, free, NULL); if(!start) ERRRET(errno, NULL); ctqueue_waitpush(working, start); - return res; } diff --git a/src/shared.c b/src/shared.c index 1310ef2..2397bd6 100644 --- a/src/shared.c +++ b/src/shared.c @@ -105,10 +105,10 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { ERR_rwbuf: if(___VXGG___VERBOSE_ERRORS___) { switch (eflag) { - case 1: WARN(errno, "Could not reallocate enough space for lstr",); - case 2: WARN(errno, "Ran into a read() error",); - case 3: WARN(errno, "Could not shrink lstr after reading buffer",); - default: WARN(errno, "Ran into some error",); + case 1: WARN(errno, "Could not reallocate enough space for lstr",); break; + case 2: WARN(errno, "Ran into a read() error",); break; + case 3: WARN(errno, "Could not shrink lstr after reading buffer",); break; + default: WARN(errno, "Ran into some error",); break; } } free(lstr); -- cgit v1.2.3