If this were a perf-critical path I'd be more concerned about the cost of
calling a function, but it isn't and I'm not.  In most of these cases, right
after malloc()ing a page-sized buffer it then makes a string of syscalls.  In
the ones I see, what's really going on is that it's just trying to allocate a
chunk of memory in a VM-friendly size; it then does a page-sized read, write
or similar, and doesn't extend the buffer if a page turns out to be too small.
I'm a little dubious about this code -- while its buffer edits are for the
most part properly bounds-checked, I don't see any particular guarantee that
one page is enough for what's being copied in; the authors seem simply to have
chosen it as a "large enough" amount.

Be that as it may, Steve's correct that the PAGE_SIZE constant isn't a good
way to get the page size, since it can't accomodate variable page size
architectures (sparc, mips).  Using it as a "largeish buffer the VM can
easily allocate" isn't even a good strategy once the malloc overhead is taken
into account, to say nothing of HUGETLB's 4MB page sizes, e.g.

However, concerning my own patch, I find the manpage warning that SUSv2
considers getpagesize() a deprecated call; the SUSv3 spec doesn't list it at
all.  What it does list is sysconf(), where the equivalent call is
sysconf(_SC_PAGE_SIZE).  What was wrong with getpagesize() I don't know.

I'm attaching an updated patch which uses sysconf(), though I think a better
fix would be for upstream to be less sloppy about their buffers.

-- 
Devin  \ aqua(at)devin.com, IRC:Requiem; http://www.devin.com
Carraway \ 1024D/E9ABFCD2: 13E7 199E DD1E 65F0 8905 2E43 5395 CA0D E9AB FCD2
--- libselinux-1.30.orig/src/canonicalize_context.c
+++ libselinux-1.30/src/canonicalize_context.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -23,7 +22,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/compute_av.c
+++ libselinux-1.30/src/compute_av.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -26,7 +25,7 @@
        if (fd < 0) 
                return -1;
 
-       len = PAGE_SIZE;
+       len = sysconf(_SC_PAGE_SIZE);
        buf = malloc(len);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/compute_create.c
+++ libselinux-1.30/src/compute_create.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -25,7 +24,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/compute_member.c
+++ libselinux-1.30/src/compute_member.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -25,7 +24,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/compute_relabel.c
+++ libselinux-1.30/src/compute_relabel.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -25,7 +24,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/compute_user.c
+++ libselinux-1.30/src/compute_user.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 #include <limits.h>
@@ -26,7 +25,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/enabled.c
+++ libselinux-1.30/src/enabled.c
@@ -5,7 +5,6 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <limits.h>
-#include <asm/page.h>
 #include <stdio.h>
 #include "policy.h"
 
@@ -22,7 +21,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                enabled = -1;
--- libselinux-1.30.orig/src/getcon.c
+++ libselinux-1.30/src/getcon.c
@@ -4,7 +4,6 @@
 #include "selinux_internal.h"
 #include <stdlib.h>
 #include <errno.h>
-#include <asm/page.h>
 #include "policy.h"
 
 int getcon_raw(security_context_t *context)
@@ -18,7 +17,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/getexeccon.c
+++ libselinux-1.30/src/getexeccon.c
@@ -3,7 +3,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 
@@ -18,7 +17,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/getfscreatecon.c
+++ libselinux-1.30/src/getfscreatecon.c
@@ -3,7 +3,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 
@@ -18,7 +17,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/getpidcon.c
+++ libselinux-1.30/src/getpidcon.c
@@ -4,7 +4,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <asm/page.h>
 #include "selinux_internal.h"
 #include "policy.h"
 
@@ -22,7 +21,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/getprevcon.c
+++ libselinux-1.30/src/getprevcon.c
@@ -4,7 +4,6 @@
 #include "selinux_internal.h"
 #include <stdlib.h>
 #include <errno.h>
-#include <asm/page.h>
 #include "policy.h"
 
 int getprevcon_raw(security_context_t *context)
@@ -18,7 +17,7 @@
        if (fd < 0)
                return -1;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf) {
                ret = -1;
--- libselinux-1.30.orig/src/init.c
+++ libselinux-1.30/src/init.c
@@ -4,7 +4,6 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <ctype.h>
-#include <asm/page.h>
 #include <stdio.h>
 #include <dlfcn.h>
 
@@ -27,7 +26,7 @@
        if (!fp)
                return;
 
-       size = PAGE_SIZE;
+       size = sysconf(_SC_PAGE_SIZE);
        buf = malloc(size);
        if (!buf)
                goto out;

Attachment: signature.asc
Description: Digital signature

Reply via email to