summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYi Chou <yich@google.com>2021-12-30 14:57:47 +0800
committerCommit Bot <commit-bot@chromium.org>2022-01-10 05:44:21 +0000
commit58efaacd8ad2a4020fbb0dd2dceae2fc8897f6db (patch)
tree5b778956001705f9643d725377b72fa801458835
parente5707317e85f7e8d79c8d9bcddc582ec00f97b69 (diff)
downloadvboot-58efaacd8ad2a4020fbb0dd2dceae2fc8897f6db.tar.gz
crossystem: Add flock to prevent race conditions
The crossystem command would have potential race conditions when we calling this command in parallel. Add a flock should prevent this issue. BUG=b:172876417 BRANCH=none TEST=Firing multiple crossystem commands, and check result Signed-off-by: Yi Chou <yich@google.com> Change-Id: Ia9f8a73d5fd762c79088f350ea05d0dd540f3fc3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3358668 Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: Leo Lai <cylai@google.com>
-rw-r--r--host/lib/crossystem.c74
1 files changed, 69 insertions, 5 deletions
diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c
index 02443b15..1c2415d5 100644
--- a/host/lib/crossystem.c
+++ b/host/lib/crossystem.c
@@ -4,9 +4,11 @@
*/
#include <ctype.h>
+#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
+#include <sys/file.h>
#include <unistd.h>
#include "2api.h"
@@ -22,6 +24,9 @@
#include "subprocess.h"
#include "vboot_struct.h"
+/* Filename for crossystem lock */
+#define CROSSYSTEM_LOCK_PATH "/run/lock/crossystem.lock"
+
/* Filename for kernel command line */
#define KERNEL_CMDLINE_PATH "/proc/cmdline"
@@ -92,6 +97,32 @@ int FwidStartsWith(const char *start)
return 0 == strncmp(fwid, start, strlen(start));
}
+/* Acquire the lock for crossystem SetSystemProperty call. */
+static int AcquireCrossystemLock(void)
+{
+ int lock_fd;
+
+ lock_fd = open(CROSSYSTEM_LOCK_PATH, O_RDWR | O_CREAT, 0600);
+ if (lock_fd < 0)
+ return -1;
+
+ if (flock(lock_fd, LOCK_EX) < 0)
+ return -1;
+
+ return lock_fd;
+}
+
+/* Release the lock for crossystem SetSystemProperty call. */
+static int ReleaseCrossystemLock(int lock_fd)
+{
+ if (flock(lock_fd, F_UNLCK) < 0)
+ return -1;
+
+ close(lock_fd);
+
+ return 0;
+}
+
static struct vb2_context *get_fake_context(void)
{
static uint8_t fake_workbuf[sizeof(struct vb2_shared_data) + 16]
@@ -488,8 +519,7 @@ int VbGetSystemPropertyInt(const char *name)
return value;
}
-const char *VbGetSystemPropertyString(const char *name, char *dest,
- size_t size)
+const char *VbGetSystemPropertyString(const char *name, char *dest, size_t size)
{
/* Check for HWID override via cros_config */
if (!strcasecmp(name, "hwid")) {
@@ -552,8 +582,7 @@ const char *VbGetSystemPropertyString(const char *name, char *dest,
return NULL;
}
-
-int VbSetSystemPropertyInt(const char *name, int value)
+static int VbSetSystemPropertyIntInternal(const char *name, int value)
{
/* Check architecture-dependent properties first */
@@ -673,7 +702,25 @@ int VbSetSystemPropertyInt(const char *name, int value)
return -1;
}
-int VbSetSystemPropertyString(const char* name, const char* value)
+int VbSetSystemPropertyInt(const char *name, int value)
+{
+ int result = -1;
+ int lock_fd;
+
+ lock_fd = AcquireCrossystemLock();
+ if (lock_fd < 0)
+ return -1;
+
+ result = VbSetSystemPropertyIntInternal(name, value);
+
+ if (ReleaseCrossystemLock(lock_fd) < 0)
+ return -1;
+
+ return result;
+}
+
+static int VbSetSystemPropertyStringInternal(const char *name,
+ const char *value)
{
/* Chain to architecture-dependent properties */
if (0 == VbSetArchPropertyString(name, value))
@@ -725,6 +772,23 @@ int VbSetSystemPropertyString(const char* name, const char* value)
return -1;
}
+int VbSetSystemPropertyString(const char *name, const char *value)
+{
+ int result = -1;
+ int lock_fd;
+
+ lock_fd = AcquireCrossystemLock();
+ if (lock_fd < 0)
+ return -1;
+
+ result = VbSetSystemPropertyStringInternal(name, value);
+
+ if (ReleaseCrossystemLock(lock_fd) < 0)
+ return -1;
+
+ return result;
+}
+
/**
* Get index of the last valid VBNV entry in an EEPROM.
*