git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/6] maintenance: add get_random_minute()

From
Taylor Blau <me@ttaylorr.com>
Date
Aug 8, 2023, 14:48 UTC
Message-ID
<ZNJVycLBUIXqZ0Xb@nand.local>
In-Reply-To
<xmqqzg329ycm.fsf@gitster.g>
On Mon, Aug 07, 2023 at 05:22:49PM -0700, Junio C Hamano wrote:
Show 18 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
> > Taylor Blau <me@ttaylorr.com> writes:
> >
> >> I was wondering where else we call srand() within Git, and it looks like
> >> the only other spot is in `lock_file_timeout()`.
> >
> > lock_file_timeout() should be updated to match git_mkstemps_mode(),
> > which was taught to use the csprng_bytes() function with 47efda96
> > (wrapper: use a CSPRNG to generate random file names, 2022-01-17),
> > and this new caller may want to do so as well, perhaps?  I dunno,
> > but the caller then does not have to worry about "initializing it
> > just once".
>
> Of course, the obvious downside is that crypto-secure one may be,
> unlike for its use in mkstemps(), way overkill for lockfiles and
> cron dispersion purposes, as these codepaths are not on the target
> surface.

I think that's an acceptable price to pay here, since we can drop the code to remember whether or not srand() has been called or not. Here's a patch that we could take in that direction:

--- 8< ---
Subject: [PATCH] lockfile.c: use a CSPRNG to generate backoff milliseconds

Upon failing to acquire the lockfile, `lock_file_timeout()` will try again with an exponential backoff. This backoff includes some noise as a multiplier over the default backoff behavior ranging from [0.75, 1.25].

It generates this noise via rand(3). Using a non-cryptographic source of randomness here is OK, since a more trivial attack vector (holding the file open via an external process for longer than the value of `timeout_ms`) is easier to exploit.

That all said, `lock_file_timeout()` initializes the PRNG with `srand()`. This has a couple of downsides:

  - lock_file_timeout() needs to remember whether or not the PRNG has
    or hasn't been seeded.
  - If a different function also calls `srand()`, the PRNG may start
    generating repeated values (if that caller also initialized the PRNG
    with `getpid()`).

Let's avoid both of these by using `csprng_bytes()`, in a similar spirit as 47efda967c (wrapper: use a CSPRNG to generate random file names, 2022-01-17).

Using a CSPRNG is definitely overkill for noising a backoff window, but it avoids the concerns about calling `srand()`, so let's use it here, too.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 lockfile.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 1d5ed01682..6587d407f4 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -107,22 +107,17 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
 	int n = 1;
 	int multiplier = 1;
 	long remaining_ms = 0;
-	static int random_initialized = 0;

 	if (timeout_ms == 0)
 		return lock_file(lk, path, flags, mode);

-	if (!random_initialized) {
-		srand((unsigned int)getpid());
-		random_initialized = 1;
-	}
-
 	if (timeout_ms > 0)
 		remaining_ms = timeout_ms;

 	while (1) {
 		long backoff_ms, wait_ms;
 		int fd;
+		uint64_t rand;

 		fd = lock_file(lk, path, flags, mode);

@@ -135,7 +130,10 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,

 		backoff_ms = multiplier * INITIAL_BACKOFF_MS;
 		/* back off for between 0.75*backoff_ms and 1.25*backoff_ms */
-		wait_ms = (750 + rand() % 500) * backoff_ms / 1000;
+		if (csprng_bytes(&rand, sizeof(uint64_t)) < 0)
+			return error_errno(_("unable to get random bytes for"
+					     "lockfile backoff"));
+		wait_ms = (750 + rand % 500) * backoff_ms / 1000;
 		sleep_millisec(wait_ms);
 		remaining_ms -= wait_ms;

--
2.42.0.rc0.26.g802d811bac.dirty

--- >8 ---

Thanks,
Taylor
Previous: Junio C HamanoNext: Junio C Hamano
Message 8 of 47 in “maintenance: schedule maintenance on a random minute”
  1. 0/6 maintenance: schedule maintenance on a random minuteDerrick Stolee via GitGitGadget, Aug 7, 2023
  2. 2/6 maintenance: use random minute in launchctl schedulerDerrick Stolee via GitGitGadget, Aug 7, 2023
  3. Taylor BlauAug 7, 2023
  4. 1/6 maintenance: add get_random_minute()Derrick Stolee via GitGitGadget, Aug 7, 2023
  5. Taylor BlauAug 7, 2023
  6. Junio C HamanoAug 7, 2023
  7. Junio C HamanoAug 8, 2023
  8. Taylor BlauAug 8, 2023
  9. Junio C HamanoAug 8, 2023
  10. Junio C HamanoAug 8, 2023
  11. Taylor BlauAug 8, 2023
  12. Derrick StoleeAug 8, 2023
  13. Taylor BlauAug 8, 2023
  14. Derrick StoleeAug 9, 2023
  15. Junio C HamanoAug 9, 2023
  16. Taylor BlauAug 9, 2023
  17. 3/6 maintenance: use random minute in Windows schedulerDerrick Stolee via GitGitGadget, Aug 7, 2023
  18. 4/6 maintenance: use random minute in cron schedulerDerrick Stolee via GitGitGadget, Aug 7, 2023
  19. 5/6 maintenance: swap method locationsDerrick Stolee via GitGitGadget, Aug 7, 2023
  20. Taylor BlauAug 7, 2023
  21. 6/6 maintenance: use random minute in systemd schedulerDerrick Stolee via GitGitGadget, Aug 7, 2023
  22. Taylor BlauAug 7, 2023
  23. Derrick StoleeAug 8, 2023
  24. Taylor BlauAug 8, 2023
  25. Phillip WoodAug 8, 2023
  26. Derrick StoleeAug 8, 2023
  27. Derrick StoleeAug 8, 2023
  28. Phillip WoodAug 9, 2023
  29. Phillip WoodAug 8, 2023
  30. Phillip WoodAug 8, 2023
  31. Derrick StoleeAug 8, 2023
  32. Derrick StoleeAug 8, 2023
  33. Phillip WoodAug 9, 2023
  34. 0/8 maintenance: schedule maintenance on a random minuteDerrick Stolee via GitGitGadget, Aug 10, 2023
  35. 1/8 maintenance: add get_random_minute()Derrick Stolee via GitGitGadget, Aug 10, 2023
  36. Taylor BlauAug 10, 2023
  37. 2/8 maintenance: use random minute in launchctl schedulerDerrick Stolee via GitGitGadget, Aug 10, 2023
  38. 4/8 maintenance: use random minute in cron schedulerDerrick Stolee via GitGitGadget, Aug 10, 2023
  39. 3/8 maintenance: use random minute in Windows schedulerDerrick Stolee via GitGitGadget, Aug 10, 2023
  40. 5/8 maintenance: swap method locationsDerrick Stolee via GitGitGadget, Aug 10, 2023
  41. 7/8 maintenance: fix systemd schedule overlapsDerrick Stolee via GitGitGadget, Aug 10, 2023
  42. Junio C HamanoAug 10, 2023
  43. Phillip WoodAug 14, 2023
  44. 6/8 maintenance: use random minute in systemd schedulerDerrick Stolee via GitGitGadget, Aug 10, 2023
  45. Phillip WoodAug 14, 2023
  46. 8/8 maintenance: update schedule before configDerrick Stolee via GitGitGadget, Aug 10, 2023
  47. Phillip WoodAug 14, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.