c++ - Abort trap 6 when returning from main in OS X but NOT on linux -
i have program seems run fine on linux (ubuntu 14.04), when running on os x (10.11.6) abort trap 6. i've attached code suspect problem not tied specific code. code class project, i'm not trying crack passwords or anything.
here's code, believe important stuff happens in main.
#include <openssl/aes.h> #include <openssl/evp.h> #include <openssl/conf.h> #include <openssl/err.h> #define key_bytes key_length/8 #define key_length 128 unsigned char* h(unsigned char* p, unsigned char* hp); void handleerrors(void); int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext); //assumes we've padded zeros unsigned char* h(unsigned char* p, unsigned char *hp){ encrypt((unsigned char*)"0000000000000000", key_bytes, p , (unsigned char*)"0000000000000000", hp); return hp; } void handleerrors(void) { printf("panic!!\n"); //err_print_errors_fp(stderr); //sg: throw real error fool! abort(); } //sg: stolen evp man page int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { evp_cipher_ctx *ctx; int len; int ciphertext_len; /* create , initialise context */ if(!(ctx = evp_cipher_ctx_new())) handleerrors(); /* initialise encryption operation. important - ensure use key * , iv size appropriate cipher * in example using 256 bit aes (i.e. 256 bit key). * iv size *most* modes same block size. aes * 128 bits */ if(1 != evp_encryptinit_ex(ctx, evp_aes_128_cbc(), null, key, iv)) handleerrors(); /* provide message encrypted, , obtain encrypted output. * evp_encryptupdate can called multiple times if necessary */ if(1 != evp_encryptupdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleerrors(); ciphertext_len = len; /* finalise encryption. further ciphertext bytes may written @ * stage. */ if(1 != evp_encryptfinal_ex(ctx, ciphertext + len, &len)) handleerrors(); ciphertext_len += len; /* clean */ evp_cipher_ctx_free(ctx); return ciphertext_len; } int main(){ /* initialise library */ err_load_crypto_strings(); openssl_add_all_algorithms(); openssl_config(null); evp_cipher_ctx *ctx; unsigned char hp[key_bytes]; /* create , initialise context */ if(!(ctx = evp_cipher_ctx_new())) handleerrors(); h((unsigned char*) "1111111111111111", hp); for(int = 0; < key_bytes; i++){ printf("h(%i) = %x\n", i, hp[i]); } return 0; }
when run on linux following (which expect)
h(0) = 10 h(1) = df h(2) = c1 h(3) = b5 h(4) = f6 h(5) = 6c h(6) = fd h(7) = 6a h(8) = 1d h(9) = c4 h(10) = 6d h(11) = 66 h(12) = 90 h(13) = 7b h(14) = ee h(15) = b1
however when run on os x following:
h(0) = 10 h(1) = df h(2) = c1 h(3) = b5 h(4) = f6 h(5) = 6c h(6) = fd h(7) = 6a h(8) = 1d h(9) = c4 h(10) = 6d h(11) = 66 h(12) = 90 h(13) = 7b h(14) = ee h(15) = b1 abort trap: 6
when pop gdb following
(gdb) r starting program: /users/sgillen/code/457/proj3/a.out h(0) = 10 h(1) = df h(2) = c1 h(3) = b5 h(4) = f6 h(5) = 6c h(6) = fd h(7) = 6a h(8) = 1d h(9) = c4 h(10) = 6d h(11) = 66 h(12) = 90 h(13) = 7b h(14) = ee h(15) = b1 program received signal sigabrt, aborted. 0x00007fff93150f06 in __pthread_kill () /usr/lib/system/libsystem_kernel.dylib (gdb) #0 0x00007fff93150f06 in __pthread_kill () /usr/lib/system/libsystem_kernel.dylib #1 0x00007fff97b374ec in pthread_kill () /usr/lib/system/libsystem_pthread.dylib #2 0x00007fff9ba8077f in __abort () /usr/lib/system/libsystem_c.dylib #3 0x00007fff9ba8105e in __stack_chk_fail () /usr/lib/system/libsystem_c.dylib #4 0x0000000100000ea9 in main () @ gen_table.cpp:90
not sure how line numbers on stack overflow, line 90 of gen_table.cpp last return 0 in main.
i compile code following if that's relevant.
clang -wall -std=c++11 gen_table.cpp -i/usr/local/opt/openssl/include/ -lcrypto -lssl -g
any appreciated thank you!
i found answer question figured i'd post answer else somehow runs same problem. issue was overwriting own stack. encryption function using writing 32 bytes hp (which 16 byte unsigned char living on stack). i'd destroy own stack not write memory not owned process. resulted in no seg faults when program tried return there problems. exact thing killed me changed depending on how compiled code.
i'm surprised valgrind didn't catch this. , still don't know why seemed work fine on linux when compiled clang (compiled g++ got stack-smashing detected error).
edit: clear solution fix implementation of encrypt writes 16 bytes. did commenting out evp_encryptfinal_ex call.
Comments
Post a Comment