/* paxmodule.c: this file is part of the elfix package Copyright (C) 2011, 2012 Anthony G. Basile This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #include #ifdef PTPAX #include #endif #ifdef NEED_PAX_DECLS #define PT_PAX_FLAGS 0x65041580 /* Indicates PaX flag markings */ #define PF_PAGEEXEC (1 << 4) /* Enable PAGEEXEC */ #define PF_NOPAGEEXEC (1 << 5) /* Disable PAGEEXEC */ #define PF_SEGMEXEC (1 << 6) /* Enable SEGMEXEC */ #define PF_NOSEGMEXEC (1 << 7) /* Disable SEGMEXEC */ #define PF_MPROTECT (1 << 8) /* Enable MPROTECT */ #define PF_NOMPROTECT (1 << 9) /* Disable MPROTECT */ #define PF_RANDEXEC (1 << 10) /* DEPRECATED: Enable RANDEXEC */ #define PF_NORANDEXEC (1 << 11) /* DEPRECATED: Disable RANDEXEC */ #define PF_EMUTRAMP (1 << 12) /* Enable EMUTRAMP */ #define PF_NOEMUTRAMP (1 << 13) /* Disable EMUTRAMP */ #define PF_RANDMMAP (1 << 14) /* Enable RANDMMAP */ #define PF_NORANDMMAP (1 << 15) /* Disable RANDMMAP */ #endif #ifdef XTPAX #include #define PAX_NAMESPACE "user.pax.flags" #endif #define FLAGS_SIZE 6 static PyObject * pax_getflags(PyObject *, PyObject *); static PyObject * pax_setbinflags(PyObject *, PyObject *); static PyObject * pax_setstrflags(PyObject *, PyObject *); #ifdef XTPAX static PyObject * pax_deletextpax(PyObject *, PyObject *); #endif static PyMethodDef PaxMethods[] = { {"getflags", pax_getflags, METH_VARARGS, "Get the pax flags as a string."}, {"setbinflags", pax_setbinflags, METH_VARARGS, "Set the pax flags using binary."}, {"setstrflags", pax_setstrflags, METH_VARARGS, "Set the pax flags using string."}, #ifdef XTPAX {"deletextpax", pax_deletextpax, METH_VARARGS, "Delete the XATTR_PAX field."}, #endif {NULL, NULL, 0, NULL} }; #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "pax", /* m_name */ "Module for get/set/deleting PT_PAX and XATTR_PAX flags", /* m_doc */ -1, /* m_size */ PaxMethods, /* m_methods */ NULL, /* m_reload */ NULL, /* m_traverse */ NULL, /* m_clear */ NULL, /* m_free */ }; #endif static PyObject *PaxError; PyMODINIT_FUNC #if PY_MAJOR_VERSION >= 3 PyInit_pax(void) #else initpax(void) #endif { PyObject *m; #if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&moduledef); #else m = Py_InitModule("pax", PaxMethods); #endif if (m == NULL) #if PY_MAJOR_VERSION >= 3 return NULL; #else return; #endif PaxError = PyErr_NewException("pax.PaxError", NULL, NULL); Py_INCREF(PaxError); PyModule_AddObject(m, "PaxError", PaxError); #if PY_MAJOR_VERSION >= 3 return m; #else return; #endif } #ifdef PTPAX uint16_t get_pt_flags(int fd) { Elf *elf; GElf_Phdr phdr; size_t i, phnum; uint16_t pt_flags = UINT16_MAX; if(elf_version(EV_CURRENT) == EV_NONE) { PyErr_SetString(PaxError, "get_pt_flags: library out of date"); return pt_flags; } if((elf = elf_begin(fd, ELF_C_READ_MMAP, NULL)) == NULL) { PyErr_SetString(PaxError, "get_pt_flags: elf_begin() failed"); return pt_flags; } if(elf_kind(elf) != ELF_K_ELF) { elf_end(elf); PyErr_SetString(PaxError, "get_pt_flags: elf_kind() failed: this is not an elf file."); return pt_flags; } elf_getphdrnum(elf, &phnum); for(i=0; i