From: "Baeuerle, Florian" <Florian.Baeuerle@allegion.com>
To: "barebox@lists.infradead.org" <barebox@lists.infradead.org>
Subject: [PATCH v2 1/2] recursive_action: add ACTION_SORT flag
Date: Wed, 19 Dec 2018 14:02:03 +0000 [thread overview]
Message-ID: <20181219140146.11806-1-florian.baeuerle@allegion.com> (raw)
In-Reply-To: <20181218132233.23669-1-florian.baeuerle@allegion.com>
Add a flag to sort directory entries before recursing into them.
Since this part of lib/ is used inside barebox as well as in
scripts/bareboxenv.c, we cannot easily use stringlists from lib/, which
would have made the code a bit nicer.
Further, add poison.h from kernel 4.5-rc1, which was the base for
importing linux headers under scripts/ in a883d9a. This is required for
actually using kernel linked lists within scripts/ tools.
Signed-off-by: Florian Bäuerle <florian.baeuerle@allegion.com>
---
include/libbb.h | 1 +
lib/list_sort.c | 3 +-
lib/recursive_action.c | 61 +++++++++++++++++---
scripts/bareboxenv.c | 15 +++++
scripts/include/linux/list_sort.h | 1 +
scripts/include/linux/poison.h | 93 ++++++++++++++++++++++++++++++-
6 files changed, 164 insertions(+), 10 deletions(-)
create mode 100644 scripts/include/linux/list_sort.h
diff --git a/include/libbb.h b/include/libbb.h
index a362bd32d..1f6afaa27 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -17,6 +17,7 @@ enum {
ACTION_FOLLOWLINKS = (1 << 1),
ACTION_DEPTHFIRST = (1 << 2),
/*ACTION_REVERSE = (1 << 3), - unused */
+ ACTION_SORT = (1 << 4),
};
int recursive_action(const char *fileName, unsigned flags,
diff --git a/lib/list_sort.c b/lib/list_sort.c
index b7e74f260..84c6f6465 100644
--- a/lib/list_sort.c
+++ b/lib/list_sort.c
@@ -1,7 +1,6 @@
#ifndef __BAREBOX__
#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/slab.h>
+#define EXPORT_SYMBOL(x)
#else
#include <common.h>
#include <malloc.h>
diff --git a/lib/recursive_action.c b/lib/recursive_action.c
index 345d3db0c..19e661ef1 100644
--- a/lib/recursive_action.c
+++ b/lib/recursive_action.c
@@ -13,9 +13,18 @@
#include <linux/stat.h>
#include <malloc.h>
#include <libbb.h>
+#include <xfuncs.h>
#endif
+#include <linux/list.h>
+#include <linux/list_sort.h>
+
+struct dirlist {
+ char *dirname;
+ struct list_head list;
+};
+
/*
* Walk down all the directories under the specified
* location, and do something (something specified
@@ -33,6 +42,19 @@ static int true_action(const char *fileName, struct stat *statbuf,
return 1;
}
+static int cmp_dirlist(void *priv, struct list_head *a, struct list_head *b)
+{
+ struct dirlist *ra, *rb;
+
+ if (a == b)
+ return 0;
+
+ ra = list_entry(a, struct dirlist, list);
+ rb = list_entry(b, struct dirlist, list);
+
+ return strcmp(ra->dirname, rb->dirname);
+}
+
/* fileAction return value of 0 on any file in directory will make
* recursive_action() return 0, but it doesn't stop directory traversal
* (fileAction/dirAction will be called on each file).
@@ -55,9 +77,12 @@ int recursive_action(const char *fileName,
{
struct stat statbuf;
unsigned follow;
+ unsigned sort;
int status;
DIR *dir;
struct dirent *next;
+ struct dirlist *entry, *entry_tmp;
+ LIST_HEAD(dirs);
if (!fileAction) fileAction = true_action;
if (!dirAction) dirAction = true_action;
@@ -106,20 +131,42 @@ int recursive_action(const char *fileName,
/* To trigger: "find -exec rm -rf {} \;" */
goto done_nak_warn;
}
+ sort = flags & ACTION_SORT;
status = 1;
while ((next = readdir(dir)) != NULL) {
- char *nextFile;
-
- nextFile = concat_subpath_file(fileName, next->d_name);
+ char *nextFile = concat_subpath_file(fileName, next->d_name);
if (nextFile == NULL)
continue;
- /* now descend into it, forcing recursion. */
- if (!recursive_action(nextFile, flags | ACTION_RECURSE,
- fileAction, dirAction, userData, depth+1)) {
- status = 0;
+
+ if (sort) {
+ struct dirlist *e = xmalloc(sizeof(*e));
+ e->dirname = xstrdup(next->d_name);
+ list_add(&e->list, &dirs);
+ } else {
+ /* descend into it, forcing recursion. */
+ if (!recursive_action(nextFile, flags | ACTION_RECURSE,
+ fileAction, dirAction, userData, depth+1)) {
+ status = 0;
+ }
}
free(nextFile);
}
+
+ if (sort) {
+ list_sort(NULL, &dirs, &cmp_dirlist);
+
+ list_for_each_entry_safe(entry, entry_tmp, &dirs, list)
+ {
+ /* descend into it, forcing recursion. */
+ if (!recursive_action(entry->dirname, flags | ACTION_RECURSE,
+ fileAction, dirAction, userData, depth+1)) {
+ status = 0;
+ }
+
+ list_del(&entry->list);
+ free(entry->dirname);
+ }
+ }
closedir(dir);
if ((flags & ACTION_DEPTHFIRST) &&
!dirAction(fileName, &statbuf, userData, depth)) {
diff --git a/scripts/bareboxenv.c b/scripts/bareboxenv.c
index 249e65b25..e95bdeaa5 100644
--- a/scripts/bareboxenv.c
+++ b/scripts/bareboxenv.c
@@ -34,6 +34,7 @@
#include "compiler.h"
#define debug(...)
+#define printk_once(...)
/* Find out if the last character of a string matches the one given.
* Don't underrun the buffer if the string length is 0.
@@ -54,6 +55,7 @@ enum {
ACTION_FOLLOWLINKS = (1 << 1),
ACTION_DEPTHFIRST = (1 << 2),
/*ACTION_REVERSE = (1 << 3), - unused */
+ ACTION_SORT = (1 << 4),
};
int recursive_action(const char *fileName, unsigned flags,
@@ -95,6 +97,19 @@ static char *concat_subpath_file(const char *path, const char *f)
return concat_path_file(path, f);
}
+static char *xstrdup(const char *s)
+{
+ int len = strlen(s) + 1;
+ char *d = xmalloc(len);
+
+ memcpy(d, s, len);
+
+ return d;
+}
+
+#include <linux/list.h>
+#include <linux/list_sort.h>
+#include "../lib/list_sort.c"
#include "../lib/recursive_action.c"
#include "../include/envfs.h"
#include "../crypto/crc32.c"
diff --git a/scripts/include/linux/list_sort.h b/scripts/include/linux/list_sort.h
new file mode 100644
index 000000000..be889c9ae
--- /dev/null
+++ b/scripts/include/linux/list_sort.h
@@ -0,0 +1 @@
+#include <../../include/linux/list_sort.h>
diff --git a/scripts/include/linux/poison.h b/scripts/include/linux/poison.h
index 0c27bdf14..15927ebc2 100644
--- a/scripts/include/linux/poison.h
+++ b/scripts/include/linux/poison.h
@@ -1 +1,92 @@
-#include "../../../include/linux/poison.h"
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_POISON_H
+#define _LINUX_POISON_H
+
+/********** include/linux/list.h **********/
+
+/*
+ * Architectures might want to move the poison pointer offset
+ * into some well-recognized area such as 0xdead000000000000,
+ * that is also not mappable by user-space exploits:
+ */
+#ifdef CONFIG_ILLEGAL_POINTER_VALUE
+# define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
+#else
+# define POISON_POINTER_DELTA 0
+#endif
+
+/*
+ * These are non-NULL pointers that will result in page faults
+ * under normal circumstances, used to verify that nobody uses
+ * non-initialized list entries.
+ */
+#define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA)
+#define LIST_POISON2 ((void *) 0x200 + POISON_POINTER_DELTA)
+
+/********** include/linux/timer.h **********/
+/*
+ * Magic number "tsta" to indicate a static timer initializer
+ * for the object debugging code.
+ */
+#define TIMER_ENTRY_STATIC ((void *) 0x300 + POISON_POINTER_DELTA)
+
+/********** mm/debug-pagealloc.c **********/
+#ifdef CONFIG_PAGE_POISONING_ZERO
+#define PAGE_POISON 0x00
+#else
+#define PAGE_POISON 0xaa
+#endif
+
+/********** mm/page_alloc.c ************/
+
+#define TAIL_MAPPING ((void *) 0x400 + POISON_POINTER_DELTA)
+
+/********** mm/slab.c **********/
+/*
+ * Magic nums for obj red zoning.
+ * Placed in the first word before and the first word after an obj.
+ */
+#define RED_INACTIVE 0x09F911029D74E35BULL /* when obj is inactive */
+#define RED_ACTIVE 0xD84156C5635688C0ULL /* when obj is active */
+
+#define SLUB_RED_INACTIVE 0xbb
+#define SLUB_RED_ACTIVE 0xcc
+
+/* ...and for poisoning */
+#define POISON_INUSE 0x5a /* for use-uninitialised poisoning */
+#define POISON_FREE 0x6b /* for use-after-free poisoning */
+#define POISON_END 0xa5 /* end-byte of poisoning */
+
+/********** arch/$ARCH/mm/init.c **********/
+#define POISON_FREE_INITMEM 0xcc
+
+/********** arch/ia64/hp/common/sba_iommu.c **********/
+/*
+ * arch/ia64/hp/common/sba_iommu.c uses a 16-byte poison string with a
+ * value of "SBAIOMMU POISON\0" for spill-over poisoning.
+ */
+
+/********** fs/jbd/journal.c **********/
+#define JBD_POISON_FREE 0x5b
+#define JBD2_POISON_FREE 0x5c
+
+/********** drivers/base/dmapool.c **********/
+#define POOL_POISON_FREED 0xa7 /* !inuse */
+#define POOL_POISON_ALLOCATED 0xa9 /* !initted */
+
+/********** drivers/atm/ **********/
+#define ATM_POISON_FREE 0x12
+#define ATM_POISON 0xdeadbeef
+
+/********** kernel/mutexes **********/
+#define MUTEX_DEBUG_INIT 0x11
+#define MUTEX_DEBUG_FREE 0x22
+#define MUTEX_POISON_WW_CTX ((void *) 0x500 + POISON_POINTER_DELTA)
+
+/********** lib/flex_array.c **********/
+#define FLEX_ARRAY_FREE 0x6c /* for use-after-free poisoning */
+
+/********** security/ **********/
+#define KEY_DESTROY 0xbd
+
+#endif
--
2.19.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2018-12-19 14:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-13 15:47 bareboxenv -s: output depends on filesystem Baeuerle, Florian
2018-12-17 10:53 ` Sascha Hauer
2018-12-18 13:22 ` [PATCH 1/2] recursive_action: add ACTION_SORT flag Baeuerle, Florian
2018-12-18 13:22 ` [PATCH 2/2] envfs: new flag for sorting env before saving Baeuerle, Florian
2018-12-19 12:42 ` [PATCH 1/2] recursive_action: add ACTION_SORT flag Baeuerle, Florian
2018-12-19 14:02 ` Baeuerle, Florian [this message]
2018-12-19 14:02 ` [PATCH v2 2/2] envfs: new flag for sorting env before saving Baeuerle, Florian
2019-01-03 10:59 ` [PATCH v2 1/2] recursive_action: add ACTION_SORT flag Sascha Hauer
2019-01-03 11:53 ` Baeuerle, Florian
2019-01-07 7:58 ` s.hauer
2019-01-08 9:47 ` Baeuerle, Florian
2019-01-08 9:48 ` [PATCH 1/2] envfs: fix problem #1 Baeuerle, Florian
2019-01-08 9:48 ` [PATCH 2/2] envfs: fix problem #2 Baeuerle, Florian
2019-01-08 15:40 ` [PATCH v2 1/2] recursive_action: add ACTION_SORT flag s.hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181219140146.11806-1-florian.baeuerle@allegion.com \
--to=florian.baeuerle@allegion.com \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox