buffer-overflow

Which stream does “stack smashing detected” message get printed to?

戏子无情 提交于 2019-11-28 06:09:02
问题 Consider the following very basic program, which has appeared in many forms on other questions here. #include <string.h> int main() { char message[8]; strcpy(message, "Hello, world!"); } On my system, if I put this in a file called Classic.c , compile it with no special flags and run it, I get the following output. $ gcc -o Classic Class.c $ ./Classic *** stack smashing detected ***: ./Classic terminated Aborted (core dumped) Normally, program output goes to stderr or stdout , so I expected

What C/C++ tools can check for buffer overflows? [closed]

泄露秘密 提交于 2019-11-28 04:33:29
I've been asked to maintain a large C++ codebase full of memory leaks. While poking around, I found out that we have a lot of buffer overflows that lead to the leaks (how it got this bad, I don't ever want to know). I've decided to removing the buffer overflows first. To make my bug-hunting easier, what tools can be used to check for buffer overruns? On Linux I'd use Valgrind. Consider using more modern data structures as a way of avoiding buffer overflows. Reading into a std::string won't overflow, and std::vectors are much safer than arrays. I don't know what your application is, and it's

Why does this memory address %fs:0x28 ( fs[0x28] ) have a random value?

时光毁灭记忆、已成空白 提交于 2019-11-28 04:10:46
I've written a piece of C code and I've disassembled it as well as read the registers to understand how the program works in assembly. int test(char *this){ char sum_buf[6]; strncpy(sum_buf,this,32); return 0; } The piece of my code that I've been examining is the test function. When I disassemble the output my test function I get ... 0x00000000004005c0 <+12>: mov %fs:0x28,%rax => 0x00000000004005c9 <+21>: mov %rax,-0x8(%rbp) ... stuff .. 0x00000000004005f0 <+60>: xor %fs:0x28,%rdx 0x00000000004005f9 <+69>: je 0x400600 <test+76> 0x00000000004005fb <+71>: callq 0x4004a0 <__stack_chk_fail@plt>

What is the difference between a stack overflow and buffer overflow?

孤街醉人 提交于 2019-11-28 03:01:41
What is different between stack overflow and buffer overflow in Programming ? Stack overflow refers specifically to the case when the execution stack grows beyond the memory that is reserved for it. For example, if you call a function which recursively calls itself without termination, you will cause a stack overflow as each function call creates a new stack frame and the stack will eventually consume more memory than is reserved for it. Buffer overflow refers to any case in which a program writes beyond the end of the memory allocated for any buffer (including on the heap, not just on the

Writing Secure C and Secure C Idioms

荒凉一梦 提交于 2019-11-28 02:47:49
"The average man does not want to be free. He simply wants to be safe." - H. L. Menken I am attempting to write very secure C. Below I list some of the techniques I use and ask are they as secure as I think they are. Please don't not hesitate to tear my code/preconceptions to shreds. Any answer that finds even the most trivial vulnerability or teaches me a new idea will be highly valued . Reading from a stream: According to the GNU C Programming Tutorial getline: The getline function will automatically enlarge the block of memory as needed, via the realloc function, so there is never a

Overflow over scanf(“%8s”, string)?

大憨熊 提交于 2019-11-28 00:22:16
I know it's possible to overflow ordinary code: char string[9]; scanf("%s", string). But is it possible to overflow scanf("%8s", string)? 8 is just an example. I know "%8s" works like a delimit, but I also notice when I input string longer than 8 chars, the program will terminate due to: * stack smashing detected * : ./a.out terminated ======= Backtrace: ========= ... Obviously there's a flag that detects stack smashing turned on by GCC by default. Since this is a stack smashing, then my guess is that it is still possible to overflow and execute arbitrary code. Contrary to normal overflow that

Array overflow (why does this work?)

最后都变了- 提交于 2019-11-27 22:37:26
Okay, so I was teaching my girlfriend some c++, and she wrote a program that I thought wouldn't work, but it did. It accesses one more element in the array then there is (for instance, accessing array[5] for an array of size 5). Is this an instance of a buffer overflow? My thoughts on it are that it's writing to/accessing the memory directly after the array, is this correct? Basically my question here is..why does this work? #include <iostream> using namespace std; int main() { int size; cout << "Please enter a size for the array." << endl; cin >> size; cout << endl; cout << "There are " <<

How to determine the size of an allocated C buffer? [duplicate]

浪尽此生 提交于 2019-11-27 22:23:40
This question already has an answer here: How can I get the size of a memory block allocated using malloc()? [duplicate] 9 answers I have a buffer and want to do a test to see if the buffer has sufficient capacity I.e. find number of elements I can add to the buffer. char *buffer = (char *)malloc(sizeof(char) * 10); Doing a int numElements = sizeof(buffer); does not return 10, any ideas on how I can accomplish this? buffer is just a pointer without size information. However the malloc() routine will hold the size of the allocation you made so when you free() it, it frees the right amount of

Is sscanf considered safe to use?

岁酱吖の 提交于 2019-11-27 15:57:38
问题 I have vague memories of suggestions that sscanf was bad. I know it won't overflow buffers if I use the field width specifier, so is my memory just playing tricks with me? 回答1: I think it depends on how you're using it: If you're scanning for something like int , it's fine. If you're scanning for a string, it's not (unless there was a width field I'm forgetting?). Edit : It's not always safe for scanning strings. If your buffer size is a constant, then you can certainly specify it as

How can I use strncat without buffer overflow concerns?

假装没事ソ 提交于 2019-11-27 13:46:06
I have a buffer, I am doing lot of strncat. I want to make sure I never overflow the buffer size. char buff[64]; strcpy(buff, "String 1"); strncat(buff, "String 2", sizeof(buff)); strncat(buff, "String 3", sizeof(buff)); Instead of sizeof(buff), I want to say something buff - xxx. I want to make sure I never override the buffer Take into consideration the size of the existing string and the null terminator #define BUFFER_SIZE 64 char buff[BUFFER_SIZE]; //Use strncpy strncpy(buff, "String 1", BUFFER_SIZE - 1); buff[BUFFER_SIZE - 1] = '\0'; strncat(buff, "String 2", BUFFER_SIZE - strlen(buff) -