From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RFVQU-0001NP-LB for garchives@archives.gentoo.org; Sun, 16 Oct 2011 18:27:11 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2615621C035; Sun, 16 Oct 2011 18:27:03 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id C86CC21C035 for ; Sun, 16 Oct 2011 18:27:02 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 360D41B4025 for ; Sun, 16 Oct 2011 18:27:02 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 9905080042 for ; Sun, 16 Oct 2011 18:27:01 +0000 (UTC) From: "Anthony G. Basile" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Anthony G. Basile" Message-ID: <0e550ad6305c42b2112499f74c79afd0fc2da6b2.blueness@gentoo> Subject: [gentoo-commits] proj/elfix:master commit in: scripts/ X-VCS-Repository: proj/elfix X-VCS-Files: scripts/paxmodule.c scripts/revdep-pax X-VCS-Directories: scripts/ X-VCS-Committer: blueness X-VCS-Committer-Name: Anthony G. Basile X-VCS-Revision: 0e550ad6305c42b2112499f74c79afd0fc2da6b2 Date: Sun, 16 Oct 2011 18:27:01 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 09e59a5941ef9a0f74260d6683b15d50 commit: 0e550ad6305c42b2112499f74c79afd0fc2da6b2 Author: Anthony G. Basile gentoo org> AuthorDate: Sun Oct 16 18:03:58 2011 +0000 Commit: Anthony G. Basile gentoo org> CommitDate: Sun Oct 16 18:26:45 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/elfix.git;a=3D= commit;h=3D0e550ad6 scripts/paxmodule.c: add pax_setflags --- scripts/paxmodule.c | 253 +++++++++++++++++++++++++++++++++++++++++++++= ++++++ scripts/revdep-pax | 4 + 2 files changed, 257 insertions(+), 0 deletions(-) diff --git a/scripts/paxmodule.c b/scripts/paxmodule.c index 927bb50..eac774a 100644 --- a/scripts/paxmodule.c +++ b/scripts/paxmodule.c @@ -24,9 +24,11 @@ =20 =20 static PyObject * pax_getflags(PyObject *, PyObject *); +static PyObject * pax_setflags(PyObject *, PyObject *); =20 static PyMethodDef PaxMethods[] =3D { {"getflags", pax_getflags, METH_VARARGS, "Get the pax flags."}, + {"setflags", pax_setflags, METH_VARARGS, "Set the pax flags."}, {NULL, NULL, 0, NULL} }; =20 @@ -160,3 +162,254 @@ pax_getflags(PyObject *self, PyObject *args) =20 return Py_BuildValue("s", pax_buf); } + + +static PyObject * +pax_setflags(PyObject *self, PyObject *args) +{ + const char *f_name; + int pax_flags; + int fd, sts; + + Elf *elf; + GElf_Ehdr ehdr; + uint16_t ei_flags; + + GElf_Phdr phdr; + size_t i, phnum; + + if (!PyArg_ParseTuple(args, "si", &f_name, &pax_flags)) + { + PyErr_SetString(PaxError, "pax_setflags: PyArg_ParseTuple failed"); + return NULL; + } + + if(elf_version(EV_CURRENT) =3D=3D EV_NONE) + { + PyErr_SetString(PaxError, "pax_setflags: library out of date"); + return NULL; + } + + if((fd =3D open(f_name, O_RDONLY)) < 0) + { + PyErr_SetString(PaxError, "pax_setflags: open() failed"); + return NULL; + } + + if((elf =3D elf_begin(fd, ELF_C_READ_MMAP, NULL)) =3D=3D NULL) + { + close(fd); + PyErr_SetString(PaxError, "pax_setflags: elf_begin() failed"); + return NULL; + } + + if(elf_kind(elf) !=3D ELF_K_ELF) + { + elf_end(elf); + close(fd); + PyErr_SetString(PaxError, "pax_setflags: elf_kind() failed: this is no= t an elf file."); + return NULL; + } + + + + if(gelf_getehdr(elf, &ehdr) !=3D &ehdr) + { + elf_end(elf); + close(fd); + PyErr_SetString(PaxError, "pax_setflags: gelf_getehdr() failed"); + return NULL; + } + + ei_flags =3D ehdr.e_ident[EI_PAX] + (ehdr.e_ident[EI_PAX + 1] << 8); + + //PAGEEXEC + if(pax_flags & PF_PAGEEXEC) + ei_flags &=3D ~HF_PAX_PAGEEXEC; + if(pax_flags & PF_NOPAGEEXEC) + ei_flags |=3D HF_PAX_PAGEEXEC; + if((pax_flags & PF_PAGEEXEC) && (pax_flags & PF_NOPAGEEXEC)) + ei_flags &=3D ~HF_PAX_PAGEEXEC; + + //SEGMEXEC + if(pax_flags & PF_SEGMEXEC) + ei_flags &=3D ~HF_PAX_SEGMEXEC; + if(pax_flags & PF_NOSEGMEXEC) + ei_flags |=3D HF_PAX_SEGMEXEC; + if((pax_flags & PF_SEGMEXEC) && (pax_flags & PF_NOSEGMEXEC)) + ei_flags &=3D ~HF_PAX_SEGMEXEC; + + //MPROTECT + if(pax_flags & PF_MPROTECT) + ei_flags &=3D ~HF_PAX_MPROTECT; + if(pax_flags & PF_NOMPROTECT) + ei_flags |=3D HF_PAX_MPROTECT; + if((pax_flags & PF_MPROTECT) && (pax_flags & PF_NOMPROTECT)) + ei_flags &=3D ~HF_PAX_MPROTECT; + + //EMUTRAMP + if(pax_flags & PF_EMUTRAMP) + ei_flags |=3D HF_PAX_EMUTRAMP; + if(pax_flags & PF_NOEMUTRAMP) + ei_flags &=3D ~HF_PAX_EMUTRAMP; + if((pax_flags & PF_EMUTRAMP) && (pax_flags & PF_NOEMUTRAMP)) + ei_flags &=3D ~HF_PAX_EMUTRAMP; + + //RANDMMAP + if(pax_flags & PF_RANDMMAP) + ei_flags &=3D ~HF_PAX_RANDMMAP; + if(pax_flags & PF_NORANDMMAP) + ei_flags |=3D HF_PAX_RANDMMAP; + if((pax_flags & PF_RANDMMAP) && (pax_flags & PF_NORANDMMAP)) + ei_flags &=3D ~HF_PAX_RANDMMAP; + + //RANDEXEC + if(pax_flags & PF_RANDEXEC) + ei_flags |=3D HF_PAX_RANDEXEC; + if(pax_flags & PF_NORANDEXEC) + ei_flags &=3D ~HF_PAX_RANDEXEC; + if((pax_flags & PF_RANDEXEC) && (pax_flags & PF_NORANDEXEC)) + ei_flags |=3D HF_PAX_RANDEXEC; + + + ehdr.e_ident[EI_PAX] =3D (uint8_t)ei_flags ; + ehdr.e_ident[EI_PAX + 1] =3D (uint8_t)(ei_flags >> 8) ; + + if(!gelf_update_ehdr(elf, &ehdr)) + { + elf_end(elf); + close(fd); + PyErr_SetString(PaxError, "pax_setflags: gelf_update_ehdr() failed"); + return NULL; + } + + elf_getphdrnum(elf, &phnum); + for(i=3D0; i