aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2011-10-20 14:12:31 -0400
committerAnthony G. Basile <blueness@gentoo.org>2011-10-20 14:12:31 -0400
commit1efd8ec798c7ae81e19d1b781fa119e07552d538 (patch)
treeae5809a6211a61e8d411f62e9ef8cbef76c93f99
parentscripts/paxmodule.c: add XT_PAX write support, code cleanup (diff)
downloadelfix-1efd8ec798c7ae81e19d1b781fa119e07552d538.tar.gz
elfix-1efd8ec798c7ae81e19d1b781fa119e07552d538.tar.bz2
elfix-1efd8ec798c7ae81e19d1b781fa119e07552d538.zip
src/paxctl-ng.c: clean up error handling
-rw-r--r--scripts/paxmodule.c50
-rw-r--r--src/paxctl-ng.c60
2 files changed, 18 insertions, 92 deletions
diff --git a/scripts/paxmodule.c b/scripts/paxmodule.c
index c5a7aa6..ed74430 100644
--- a/scripts/paxmodule.c
+++ b/scripts/paxmodule.c
@@ -65,9 +65,7 @@ get_pt_flags(int fd)
GElf_Phdr phdr;
size_t i, phnum;
- uint16_t pt_flags;
-
- pt_flags = UINT16_MAX;
+ uint16_t pt_flags = UINT16_MAX;
if(elf_version(EV_CURRENT) == EV_NONE)
{
@@ -93,7 +91,10 @@ get_pt_flags(int fd)
for(i=0; i<phnum; i++)
{
if(gelf_getphdr(elf, i, &phdr) != &phdr)
+ {
PyErr_SetString(PaxError, "get_pt_flags: gelf_getphdr() failed: could not get phdr.");
+ return pt_flags;
+ }
if(phdr.p_type == PT_PAX_FLAGS)
pt_flags = phdr.p_flags;
@@ -108,38 +109,9 @@ get_pt_flags(int fd)
uint16_t
get_xt_flags(int fd)
{
- uint16_t xt_flags;
-
- xt_flags = UINT16_MAX;
-
- if(fgetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t)) == -1)
- {
- /*
- // ERANGE = xattrs supported, PAX_NAMESPACE present, but wrong size
- // ENOATTR = xattrs supported, PAX_NAMESPACE not present
- if(errno == ERANGE || errno == ENOATTR)
- {
- //XT_PAX: not present or corrupted
+ uint16_t xt_flags = UINT16_MAX;
- //BEGIN: create flags
- PyErr_SetString(PaxError, "XT_PAX: creating/repairing flags");
- xt_flags = PF_NOEMUTRAMP | PF_NORANDEXEC;
- if(fsetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t), 0) == -1)
- {
- xt_flags = UINT16_MAX;
- if(errno == ENOSPC || errno == EDQUOT)
- PyErr_SetString(PaxError, "XT_PAX: access error");
- if(errno == ENOTSUP)
- PyErr_SetString(PaxError, "XT_PAX: not supported");
- }
- // END: create flags
- }
-
- // ENOTSUP = xattrs not supported
- if(errno == ENOTSUP)
- PyErr_SetString(PaxError, "XT_PAX: not supported\n");
- */
- }
+ fgetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t));
return xt_flags;
}
@@ -269,15 +241,7 @@ set_pt_flags(int fd, uint16_t pt_flags)
void
set_xt_flags(int fd, uint16_t xt_flags)
{
- if(fsetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t), 0) == -1)
- {
- /*
- if(errno == ENOSPC || errno == EDQUOT)
- PyErr_SetString(PaxError, "XT_PAX: access error");
- if(errno == ENOTSUP)
- PyErr_SetString(PaxError, "XT_PAX: not supported\n");
- */
- }
+ fsetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t), 0);
}
diff --git a/src/paxctl-ng.c b/src/paxctl-ng.c
index aaeadaf..8d9bf55 100644
--- a/src/paxctl-ng.c
+++ b/src/paxctl-ng.c
@@ -163,27 +163,18 @@ get_pt_flags(int fd)
GElf_Phdr phdr;
size_t i, phnum;
- uint16_t pt_flags;
-
- pt_flags = UINT16_MAX;
+ uint16_t pt_flags = UINT16_MAX;
if(elf_version(EV_CURRENT) == EV_NONE)
- {
error(EXIT_FAILURE, 0, "Library out of date.");
- return;
- }
if((elf = elf_begin(fd, ELF_C_READ_MMAP, NULL)) == NULL)
- {
error(EXIT_FAILURE, 0, "elf_begin() fail: %s", elf_errmsg(elf_errno()));
- return;
- }
if(elf_kind(elf) != ELF_K_ELF)
{
elf_end(elf);
error(EXIT_FAILURE, 0, "elf_kind() fail: this is not an elf file.");
- return;
}
elf_getphdrnum(elf, &phnum);
@@ -191,14 +182,16 @@ get_pt_flags(int fd)
for(i=0; i<phnum; i++)
{
if(gelf_getphdr(elf, i, &phdr) != &phdr)
+ {
+ elf_end(elf);
error(EXIT_FAILURE, 0, "gelf_getphdr(): %s", elf_errmsg(elf_errno()));
+ }
if(phdr.p_type == PT_PAX_FLAGS)
pt_flags = phdr.p_flags;
}
elf_end(elf);
-
return pt_flags;
}
@@ -206,32 +199,16 @@ get_pt_flags(int fd)
uint16_t
get_xt_flags(int fd)
{
- uint16_t xt_flags;
-
- xt_flags = UINT16_MAX;
+ uint16_t xt_flags = UINT16_MAX;
if(fgetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t)) == -1)
{
- // ERANGE = xattrs supported, PAX_NAMESPACE present, but wrong size
- // ENOATTR = xattrs supported, PAX_NAMESPACE not present
- if(errno == ERANGE || errno == ENOATTR)
- {
- printf("XT_PAX: not present or corrupted\n");
- /*
- printf("XT_PAX: creating/repairing flags\n");
- xt_flags = PF_NOEMUTRAMP | PF_NORANDEXEC;
- if(fsetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t), 0) == -1)
- {
- xt_flags = UINT16_MAX;
- if(errno == ENOSPC || errno == EDQUOT)
- printf("XT_PAX: access error\n");
- if(errno == ENOTSUP)
- printf("XT_PAX: not supported\n");
- }
- */
- }
+ if(errno == ERANGE )
+ printf("XT_PAX: corrupted\n");
+
+ if( errno == ENOATTR)
+ printf("XT_PAX: not present\n");
- // ENOTSUP = xattrs not supported
if(errno == ENOTSUP)
printf("XT_PAX: not supported\n");
}
@@ -271,9 +248,7 @@ print_flags(int fd)
flags = get_pt_flags(fd);
if( flags == UINT16_MAX )
- {
printf("PT_PAX: not found\n");
- }
else
{
memset(buf, 0, BUF_SIZE);
@@ -283,9 +258,7 @@ print_flags(int fd)
flags = get_xt_flags(fd);
if( flags == UINT16_MAX )
- {
printf("XT_PAX: not found\n");
- }
else
{
memset(buf, 0, BUF_SIZE);
@@ -413,22 +386,15 @@ set_pt_flags(int fd, uint16_t pt_flags)
size_t i, phnum;
if(elf_version(EV_CURRENT) == EV_NONE)
- {
error(EXIT_FAILURE, 0, "Library out of date.");
- return;
- }
if((elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL)) == NULL)
- {
error(EXIT_FAILURE, 0, "elf_begin() fail: %s", elf_errmsg(elf_errno()));
- return;
}
-
if(elf_kind(elf) != ELF_K_ELF)
{
elf_end(elf);
error(EXIT_FAILURE, 0, "elf_kind() fail: this is not an elf file.");
- return;
}
elf_getphdrnum(elf, &phnum);
@@ -439,7 +405,6 @@ set_pt_flags(int fd, uint16_t pt_flags)
{
elf_end(elf);
error(EXIT_FAILURE, 0, "gelf_getphdr(): %s", elf_errmsg(elf_errno()));
- return;
}
if(phdr.p_type == PT_PAX_FLAGS)
@@ -450,7 +415,6 @@ set_pt_flags(int fd, uint16_t pt_flags)
{
elf_end(elf);
error(EXIT_FAILURE, 0, "gelf_update_phdr(): %s", elf_errmsg(elf_errno()));
- return;
}
}
}
@@ -465,7 +429,7 @@ set_xt_flags(int fd, uint16_t xt_flags)
if(fsetxattr(fd, PAX_NAMESPACE, &xt_flags, sizeof(uint16_t), 0) == -1)
{
if(errno == ENOSPC || errno == EDQUOT)
- printf("XT_PAX: access error\n");
+ printf("XT_PAX: insufficient space\n");
if(errno == ENOTSUP)
printf("XT_PAX: not supported\n");
}
@@ -501,8 +465,6 @@ main( int argc, char *argv[])
uint16_t flags;
int view_flags;
- Elf *elf;
-
f_name = parse_cmd_args(argc, argv, &flags, &view_flags);
if((fd = open(f_name, O_RDWR)) < 0)