busybox: backport cve-2017-16544 fix
[oweals/openwrt.git] / package / utils / busybox / patches / 220-add_lock_util.patch
index ce663b61f51247f4bf16697d0fa377b9246967a0..4e46b74f0e786b89c3a8cab5db90c3ea156fd3f3 100644 (file)
@@ -1,46 +1,25 @@
---- a/include/applets.src.h
-+++ b/include/applets.src.h
-@@ -212,6 +212,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, 
- IF_LOAD_POLICY(APPLET(load_policy, BB_DIR_USR_SBIN, BB_SUID_DROP))
- IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
- IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
-+IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
- IF_LOGGER(APPLET(logger, BB_DIR_USR_BIN, BB_SUID_DROP))
- /* Needs to be run by root or be suid root - needs to change uid and gid: */
- IF_LOGIN(APPLET(login, BB_DIR_BIN, BB_SUID_REQUIRE))
---- a/miscutils/Config.src
-+++ b/miscutils/Config.src
-@@ -419,6 +419,12 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
-       help
-         Enables the 'hdparm -d' option to get/set using_dma flag.
-+config LOCK
-+      bool "lock"
-+      default n
-+      help
-+        Small utility for using locks in scripts
-+
- config MAKEDEVS
-       bool "makedevs"
-       default y
---- a/miscutils/Kbuild.src
-+++ b/miscutils/Kbuild.src
-@@ -29,6 +29,7 @@ lib-$(CONFIG_INOTIFYD)    += inotifyd.o
- lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
- lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
- lib-$(CONFIG_LESS)        += less.o
-+lib-$(CONFIG_LOCK)        += lock.o
- lib-$(CONFIG_MAKEDEVS)    += makedevs.o
- lib-$(CONFIG_MAN)         += man.o
- lib-$(CONFIG_MICROCOM)    += microcom.o
 --- /dev/null
 +++ b/miscutils/lock.c
-@@ -0,0 +1,135 @@
+@@ -0,0 +1,155 @@
 +/*
-+ * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
++ * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
 + *
 + * This is free software, licensed under the GNU General Public License v2.
 + */
++
++//config:config LOCK
++//config:     bool "lock"
++//config:     default n
++//config:     help
++//config:       Small utility for using locks in scripts
++
++//applet:IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
++
++//kbuild:lib-$(CONFIG_LOCK) += lock.o
++
++//usage:#define lock_trivial_usage NOUSAGE_STR
++//usage:#define lock_full_usage ""
++
 +#include <sys/types.h>
 +#include <sys/file.h>
 +#include <sys/stat.h>
 +#include <stdio.h>
 +#include "busybox.h"
 +
-+//usage:#define lock_trivial_usage NOUSAGE_STR
-+//usage:#define lock_full_usage ""
-+
 +static int unlock = 0;
 +static int shared = 0;
 +static int waitonly = 0;
++static int try_lock = 0;
 +static int fd;
 +static char *file;
 +
@@ -65,6 +42,7 @@
 +                      "       -s      Use shared locking\n"
 +                      "       -u      Unlock\n"
 +                      "       -w      Wait for the lock to become free, don't acquire lock\n"
++                      "       -n      Don't wait for the lock to become free. Fail with exit code\n"
 +                                      "\n", name);
 +      exit(1);
 +}
@@ -95,6 +73,7 @@
 +static int do_lock(void)
 +{
 +      int pid;
++      int flags;
 +      char pidstr[8];
 +
 +      if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
 +              }
 +      }
 +
-+      if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
++      flags = shared ? LOCK_SH : LOCK_EX;
++      flags |= try_lock ? LOCK_NB : 0;
++
++      if (flock(fd, flags) < 0) {
 +              fprintf(stderr, "Can't lock %s\n", file);
 +              return 1;
 +      }
 +                              case 'u':
 +                                      unlock = 1;
 +                                      break;
++                              case 'n':
++                                      try_lock = 1;
++                                      break;
 +                      }
 +              }
 +              c--;