On 10/26/2016 09:07 PM, Rob Landley wrote:
On 10/26/2016 03:12 PM, Mark Salyzyn wrote:
>From the AOSP gerrit fixing internal bug 32399196
( https://android-review.googlesource.com/#/c/295731 )

Change subject: dirtree: add DIRTREE_NOSTAT flag

Prevents a superfluous attribute request. Flag returned on callbacks,
subsequent recursive callbacks have a responsibility to issue:

fstatat(dirtree_parentfd(dirtree), dirtree->name, &dirtree->st,
         AT_SYMLINK_NOFOLLOW);
If you're going to send me git commits anyway, could you do:

   git format-patch -1 $COMMITHASH

And then attach the resulting file to the message?

Thanks,

Rob

Enclosed the two files, you might wish to remove the Bug:# footer?

-- Mark

>From 7af4d1ab401614ba8853b1acc1eecdf95d1299bc Mon Sep 17 00:00:00 2001
From: Mark Salyzyn <saly...@android.com>
Date: Wed, 26 Oct 2016 10:29:58 -0700
Subject: [PATCH] dirtree: add DIRTREE_NOSTAT flag

Prevents a superfluous attribute request. Flag returned on callbacks,
subsequent recursive callbacks have a responsibility to issue:

fstatat(dirtree_parentfd(dirtree), dirtree->name, &dirtree->st,
        AT_SYMLINK_NOFOLLOW);

to fill in stat details on file name entries of interests. This
allows for precise control over which file(s) get stat() information.

Test: manual in combination with 'ps: only stat() /proc/<pid>'
      looking for selinux getattr violations for /proc/iomem
      or /proc/sysrq-trigger.
Bug: 32399196
Change-Id: I1aa5f2104b7ddd5587ee9608012530aab46a0f66
---
 lib/dirtree.c | 20 +++++++++++---------
 lib/lib.h     |  3 +++
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/lib/dirtree.c b/lib/dirtree.c
index 8f235ed..81559d6 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -31,21 +31,23 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
   int len = 0, linklen = 0;
 
   if (name) {
-    // open code this because haven't got node to call dirtree_parentfd() on yet
-    int fd = parent ? parent->dirfd : AT_FDCWD;
-
-    if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
-      goto error;
-    if (S_ISLNK(st.st_mode)) {
-      if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
-      libbuf[linklen++]=0;
+    if (!(flags & DIRTREE_NOSTAT)) {
+      // open code this because haven't got node to call dirtree_parentfd() on yet
+      int fd = parent ? parent->dirfd : AT_FDCWD;
+
+      if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
+        goto error;
+      if (S_ISLNK(st.st_mode)) {
+        if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
+        libbuf[linklen++]=0;
+      }
     }
     len = strlen(name);
   }
   dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
   dt->parent = parent;
   if (name) {
-    memcpy(&(dt->st), &st, sizeof(struct stat));
+    if (!(flags & DIRTREE_NOSTAT)) memcpy(&(dt->st), &st, sizeof(struct stat));
     strcpy(dt->name, name);
 
     if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
diff --git a/lib/lib.h b/lib/lib.h
index 2afe558..06393fb 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -73,6 +73,9 @@ void get_optflags(void);
 #define DIRTREE_SHUTUP      16
 // Breadth first traversal, conserves filehandles at the expense of memory
 #define DIRTREE_BREADTH     32
+// Do not stat the files. st and symlink on a recurse pass will be all zero.
+// Callback can fill in these fields afterwards, likely after filename checking.
+#define DIRTREE_NOSTAT      64
 // Don't look at any more files in this directory.
 #define DIRTREE_ABORT      256
 
-- 
2.8.0.rc3.226.g39d4020

>From a29ab6ab2cc2539be778ea7b01f9fb8a903bfd21 Mon Sep 17 00:00:00 2001
From: Mark Salyzyn <saly...@android.com>
Date: Wed, 26 Oct 2016 11:01:52 -0700
Subject: [PATCH] ps: only stat() /proc/<pid>

Callback from top /proc/ node tells dirtree to hold off performing
a fstatat() call until after we hare satisfied with the name being
numerical.

Test: adb shell ps, adb logcat -b events -d -s auditd and check for
      selinux getattr for /proc/iomem or /proc/sysrq-trigger.
Bug: 32399196
Change-Id: Ib7d64601ed9db90ff877a4ad0856f20e9fa15398
---
 toys/posix/ps.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 15acc84..25b06c4 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -615,7 +615,7 @@ static int get_ps(struct dirtree *new)
 
   // Recurse one level into /proc children, skip non-numeric entries
   if (!new->parent)
-    return DIRTREE_RECURSE|DIRTREE_SHUTUP
+    return DIRTREE_RECURSE|DIRTREE_SHUTUP|DIRTREE_NOSTAT
       |(DIRTREE_SAVE*(TT.threadparent||!TT.show_process));
 
   memset(slot, 0, sizeof(tb->slot));
@@ -623,6 +623,8 @@ static int get_ps(struct dirtree *new)
   if (TT.threadparent && TT.threadparent->extra)
     if (*slot == *(((struct carveup *)TT.threadparent->extra)->slot)) return 0;
   fd = dirtree_parentfd(new);
+  // Implied policy is DIRTREE_SHUTUP, no need for error checking.
+  fstatat(fd, new->name, &new->st, AT_SYMLINK_NOFOLLOW);
 
   len = 2048;
   sprintf(buf, "%lld/stat", *slot);
-- 
2.8.0.rc3.226.g39d4020

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to