[Buildroot] [git commit] package/fakedate: avoid recursion if not first in the PATH

Arnout Vandecappelle (Essensium/Mind) arnout at mind.be
Sat Aug 28 13:56:52 UTC 2021


commit: https://git.buildroot.net/buildroot/commit/?id=0fe536b797cc0ae11bd54ed7046cd39b73780c18
branch: https://git.buildroot.net/buildroot/commit/?id=refs/heads/master

With BR2_REPRODUCIBLE, our 'fakedate' wrapper script will end up in
host/bin/date. Currently, it iterates over all the 'date' found in PATH,
until it finds one that is not the script itself, in an attempt to avoid
infinite recursion by calling itself again and again.

This heuristic works OK in Buildroot, because host/bin/ is first in the
PATH, and so that means the first entry in the PATH is skipped.

However, this is going to fail as soon as our wrapper is not the first
in the PATH. Indeed, in that situation, the current heuristic will stop
on the first 'date' in the PATH, as it is not the script itself, and
since our script was executed, that probably means the first 'date' was
itself a wrapper that ended up calling us. So, calling it again will
eventually trickle to calling us again, and thus creating the loop our
heuristic was made to avoid.

This situation currently does not occur in Buildroot, because host/bin/
is first, *and* we have no package that provide their own 'date' wrapper
during their build steps.

But when we generate an SDK with BR2_REPRODUCIBLE, then our wrapper
script will be in sdk/bin/, and there is no longer any guarantee this
comes first in the PATH, thus opening the possibility that another
buildsystem based on our SDK, but which has its own 'date' wrapper, will
trigger this infinite recursion.

We fix that by iterating, in reverse order, over all the 'date' we can
find in PATH, and when we find ourselves, then we know the one we found
in the iteration just before is the one that we should call.

'which -a' is old enough that we can expect it to be always available;
it has been present at least since Debian Squeeze, released 2011.

Signed-off-by: Yann E. MORIN <yann.morin.1998 at free.fr>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout at mind.be>
---
 package/fakedate/fakedate | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/package/fakedate/fakedate b/package/fakedate/fakedate
index a64d9b9cdf..9bef113357 100755
--- a/package/fakedate/fakedate
+++ b/package/fakedate/fakedate
@@ -19,15 +19,13 @@
 #
 
 DATE_BIN=false
-# Do not call `date' directly since it will produce an infinite recursion.
-# Instead, find path of true `date' binary.
-for P in `echo $PATH | tr ':' ' '`; do
-    if [ -x "$P/date" ]; then
-        if ! [ "$P/date" -ef "$0" ]; then
-            DATE_BIN="$P/date"
-            break;
-        fi
+# Do not call any 'date' before us in the PATH, or that would create
+# an infinite recursion.
+for date in $(which -a date |tac); do
+    if [ "${date}" -ef "$0" ]; then
+        break
     fi
+    DATE_BIN="${date}"
 done
 
 if [ -n "$SOURCE_DATE_EPOCH" ]; then


More information about the buildroot mailing list