public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Anthony G. Basile" <blueness@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/elfix:elfix-0.2.x commit in: scripts/
Date: Sat,  8 Oct 2011 18:54:07 +0000 (UTC)	[thread overview]
Message-ID: <caa17d7d9e10e0ef49b3df0d0bf1ceb007dd64d5.blueness@gentoo> (raw)

commit:     caa17d7d9e10e0ef49b3df0d0bf1ceb007dd64d5
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Thu Oct  6 04:07:33 2011 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Sat Oct  8 18:53:27 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=caa17d7d

scripts/paxmodule.c: add code to read pax flags

---
 scripts/paxmodule.c |  127 ++++++++++++++++++++++++++++++++++++++++++++++++---
 scripts/setup.py    |    3 +-
 2 files changed, 122 insertions(+), 8 deletions(-)

diff --git a/scripts/paxmodule.c b/scripts/paxmodule.c
index 03ba794..1b3e1eb 100644
--- a/scripts/paxmodule.c
+++ b/scripts/paxmodule.c
@@ -1,5 +1,28 @@
 #include <Python.h>
 
+#include <stdio.h> //remove when you remove printf
+#include <string.h>
+
+#include <gelf.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+#define HF_PAX_PAGEEXEC		1
+#define HF_PAX_EMUTRAMP		2
+#define HF_PAX_MPROTECT		4
+#define HF_PAX_RANDMMAP		8
+#define HF_PAX_RANDEXEC		16
+#define HF_PAX_SEGMEXEC		32
+
+#define EI_PAX			14	// Index to read the PaX flags into ELF header e_ident[] array
+
+#define BUF_SIZE		7	//Buffer for holding human readable flags
+
+
 static PyObject * pax_getflags(PyObject *, PyObject *);
 
 static PyMethodDef PaxMethods[] = {
@@ -27,21 +50,111 @@ initpax(void)
 static PyObject *
 pax_getflags(PyObject *self, PyObject *args)
 {
-	const char *value;
-	int sts;
+	const char *f_name;
+	int fd, sts;
+	Elf *elf;
+
+	GElf_Ehdr ehdr;
+	char ei_buf[BUF_SIZE];
+	uint16_t ei_flags;
+
+	GElf_Phdr phdr;
+	char pt_buf[BUF_SIZE];
+	char found_pt_pax;
+	size_t i, phnum;
+
+	memset(ei_buf, 0, BUF_SIZE);
+	memset(pt_buf, 0, BUF_SIZE);
 
-	if (!PyArg_ParseTuple(args, "s", &value))
+	if (!PyArg_ParseTuple(args, "s", &f_name))
+	{
+		PyErr_SetString(PaxError, "pax_getflags: PyArg_ParseTuple failed");
 		return NULL;
+	}
 
-	printf("%s\n", value);
+	if(elf_version(EV_CURRENT) == EV_NONE)
+	{
+		PyErr_SetString(PaxError, "pax_getflags: library out of date");
+		return NULL;
+	}
 
-	sts = 1;
+	if((fd = open(f_name, O_RDONLY)) < 0)
+	{
+		PyErr_SetString(PaxError, "pax_getflags: open() failed");
+		return NULL;
+	}
 
-	if (sts < 0)
+	if((elf = elf_begin(fd, ELF_C_READ_MMAP, NULL)) == NULL)
 	{
-		PyErr_SetString(PaxError, "pax_getflags failed");
+		PyErr_SetString(PaxError, "pax_getflags: elf_begin() failed");
 		return NULL;
 	}
 
+	if(elf_kind(elf) != ELF_K_ELF)
+	{
+		PyErr_SetString(PaxError, "pax_getflags: elf_kind() failed: this is not an elf file.");
+		return NULL;
+	}
+
+
+	found_pt_pax = 0;
+	elf_getphdrnum(elf, &phnum);
+	for(i=0; i<phnum; ++i)
+	{
+		if(gelf_getphdr(elf, i, &phdr) != &phdr)
+		{
+			PyErr_SetString(PaxError, "pax_getflags: gelf_getphdr() failed");
+			return NULL;
+		}
+
+		if(phdr.p_type == PT_PAX_FLAGS)
+		{
+			found_pt_pax = 1;
+
+			pt_buf[0] = phdr.p_flags & PF_PAGEEXEC ? 'P' :
+				phdr.p_flags & PF_NOPAGEEXEC ? 'p' : '-' ;
+
+			pt_buf[1] = phdr.p_flags & PF_SEGMEXEC   ? 'S' : 
+				phdr.p_flags & PF_NOSEGMEXEC ? 's' : '-';
+
+			pt_buf[2] = phdr.p_flags & PF_MPROTECT   ? 'M' :
+				phdr.p_flags & PF_NOMPROTECT ? 'm' : '-';
+
+			pt_buf[3] = phdr.p_flags & PF_EMUTRAMP   ? 'E' :
+				phdr.p_flags & PF_NOEMUTRAMP ? 'e' : '-';
+
+			pt_buf[4] = phdr.p_flags & PF_RANDMMAP   ? 'R' :
+				phdr.p_flags & PF_NORANDMMAP ? 'r' : '-';
+
+			pt_buf[5] = phdr.p_flags & PF_RANDEXEC   ? 'X' :
+				phdr.p_flags & PF_NORANDEXEC ? 'x' : '-';
+		}
+	}
+
+	if(found_pt_pax)
+		printf("PT_PAX: %s\n", pt_buf);
+	else
+	{
+		if(gelf_getehdr(elf, &ehdr) != &ehdr)
+		{
+			PyErr_SetString(PaxError, "pax_getflags: gelf_getehdr() failed");
+			return NULL;
+		}
+
+		ei_flags = ehdr.e_ident[EI_PAX] + (ehdr.e_ident[EI_PAX + 1] << 8);
+
+  		ei_buf[0] = ei_flags & HF_PAX_PAGEEXEC ? 'p' : 'P';
+		ei_buf[1] = ei_flags & HF_PAX_SEGMEXEC ? 's' : 'S';
+		ei_buf[2] = ei_flags & HF_PAX_MPROTECT ? 'm' : 'M';
+		ei_buf[3] = ei_flags & HF_PAX_EMUTRAMP ? 'E' : 'e';
+		ei_buf[4] = ei_flags & HF_PAX_RANDMMAP ? 'r' : 'R';
+		ei_buf[5] = ei_flags & HF_PAX_RANDEXEC ? 'X' : 'x';
+
+		printf("EI_PAX: %s\n", ei_buf);
+	}
+
+	elf_end(elf);
+	close(fd);
+
 	return Py_BuildValue("i", sts);
 }

diff --git a/scripts/setup.py b/scripts/setup.py
index 317efbd..77854f1 100755
--- a/scripts/setup.py
+++ b/scripts/setup.py
@@ -4,7 +4,8 @@ from distutils.core import setup, Extension
 
 module1 = Extension(
 	name='pax',
-	sources = ['paxmodule.c']
+	sources = ['paxmodule.c'],
+	libraries = ['elf'],
 )
 
 setup(



             reply	other threads:[~2011-10-08 18:55 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-08 18:54 Anthony G. Basile [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-10-17 21:09 [gentoo-commits] proj/elfix:elfix-0.2.x commit in: scripts/ Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-17 21:09 Anthony G. Basile
2011-10-12 10:49 Anthony G. Basile
2011-10-12 10:48 Anthony G. Basile
2011-10-12 10:48 Anthony G. Basile
2011-10-12 10:47 Anthony G. Basile
2011-10-12 10:47 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile
2011-10-08 18:54 Anthony G. Basile

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=caa17d7d9e10e0ef49b3df0d0bf1ceb007dd64d5.blueness@gentoo \
    --to=blueness@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox