File: writetext.c

package info (click to toggle)
paxtest 0.9.7-pre4-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 360 kB
  • ctags: 163
  • sloc: ansic: 981; makefile: 708; asm: 53; sh: 49
file content (55 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* writetext.c - Test wether a .text sections can be written
 *
 * Copyright (c)2003 by Peter Busser <peter@adamantix.org>
 * This file has been released under the GNU Public Licence version 2 or later
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include "body.h"

const char testname[] = "Writable text segments                   ";

extern int shlibtest( void );

static void sigsegv( int sig )
{
	printf( "Killed\n" );
	exit( 1 );
}

void doit( void )
{
	char *buf;
	char c;

	buf = (char*)shlibtest;

	signal( SIGSEGV, sigsegv );

	/* Try to make the text writable first by using mprotect
	 *
	 * Some people like to disable this call to make the results look
	 * better for their system.
	 *
	 * The purpose of the mprotect() here is to *really* try to write to
	 * that piece of executable memory. If you want to know whether a box
	 * can be opened or not, you try to pull it open. Just looking at it,
	 * seeing that it is closed, and therefore concluding that it cannot
	 * be opened is rather lame.
	 *
	 * But then, it is of course easier to get good paxtest results by
	 * disabling this mprotect than to fix your kernel code and userland.
	 */
	do_mprotect( buf, 4096, PROT_READ|PROT_WRITE|PROT_EXEC );

	/* Try to write something */
	*buf = 'X';

	/* It worked when the function returns */
	itworked();
}