x86: randomize brk() and RLIMIT_DATA

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Would be neat if randomized brk and setrlimit(RLIMIT_DATA, ...)
worked in a predictable way:

$ gcc brk.c -fPIC -pie -m64;./a.out;./a.out;./a.out
sbrk=0x7f721b815000 main=0x7f721af04860
sbrk succeeded (brk=0x7f721b909240)
sbrk=0x7fc3d77e2000 main=0x7fc3d66fa860
sbrk failed: Cannot allocate memory (brk=0x7fc3d77e2000)
sbrk=0x7f05b0615000 main=0x7f05af76b860
sbrk failed: Cannot allocate memory (brk=0x7f05b0615000)

Do what you love because life is too short for anything else.

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include <string.h>

int main(void)
	void *p;
	const struct rlimit rlim_data = {
		.rlim_cur = 10 << 20,
		.rlim_max = RLIM_INFINITY
	int saved_errno;

	p = sbrk(0);
	fprintf(stderr, "sbrk=%p main=%p\n", p, &main);
	if (setrlimit(RLIMIT_DATA, &rlim_data) == -1) {
		fprintf(stderr, "setrlimit failed: %s\n", strerror(errno));
		return 1;
	if (sbrk(1 << 20) == (void*)-1) {
		saved_errno = errno;
		fprintf(stderr, "sbrk failed: %s (brk=%p)\n",
			strerror(saved_errno), sbrk(0));
		return 1;
	} else {
		fprintf(stderr, "sbrk succeeded (brk=%p)\n", sbrk(0));
		return 0;

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux