All of lore.kernel.org
 help / color / mirror / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org, sandals@crustytoothpaste.net,
	lenaic@lhuard.fr, Derrick Stolee <derrickstolee@github.com>
Subject: Re: [PATCH 1/6] maintenance: add get_random_minute()
Date: Tue, 8 Aug 2023 10:48:41 -0400	[thread overview]
Message-ID: <ZNJVycLBUIXqZ0Xb@nand.local> (raw)
In-Reply-To: <xmqqzg329ycm.fsf@gitster.g>

On Mon, Aug 07, 2023 at 05:22:49PM -0700, Junio C Hamano wrote:
> 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

  reply	other threads:[~2023-08-08 16:19 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 18:51 [PATCH 0/6] maintenance: schedule maintenance on a random minute Derrick Stolee via GitGitGadget
2023-08-07 18:51 ` [PATCH 1/6] maintenance: add get_random_minute() Derrick Stolee via GitGitGadget
2023-08-07 21:20   ` Taylor Blau
2023-08-07 23:53     ` Junio C Hamano
2023-08-08  0:22       ` Junio C Hamano
2023-08-08 14:48         ` Taylor Blau [this message]
2023-08-08 16:34           ` Junio C Hamano
2023-08-08 16:49             ` Junio C Hamano
2023-08-08 20:01               ` Taylor Blau
2023-08-08 17:28     ` Derrick Stolee
2023-08-08 20:04       ` Taylor Blau
2023-08-09 12:17         ` Derrick Stolee
2023-08-09 18:50           ` Junio C Hamano
2023-08-09 20:34             ` Taylor Blau
2023-08-07 18:51 ` [PATCH 2/6] maintenance: use random minute in launchctl scheduler Derrick Stolee via GitGitGadget
2023-08-07 21:23   ` Taylor Blau
2023-08-07 18:51 ` [PATCH 3/6] maintenance: use random minute in Windows scheduler Derrick Stolee via GitGitGadget
2023-08-07 18:51 ` [PATCH 4/6] maintenance: use random minute in cron scheduler Derrick Stolee via GitGitGadget
2023-08-07 18:51 ` [PATCH 5/6] maintenance: swap method locations Derrick Stolee via GitGitGadget
2023-08-07 21:24   ` Taylor Blau
2023-08-07 18:51 ` [PATCH 6/6] maintenance: use random minute in systemd scheduler Derrick Stolee via GitGitGadget
2023-08-07 21:31   ` Taylor Blau
2023-08-08 13:49     ` Derrick Stolee
2023-08-08 20:05       ` Taylor Blau
2023-08-08  9:53   ` Phillip Wood
2023-08-08 13:03     ` Phillip Wood
2023-08-08 13:56     ` Derrick Stolee
2023-08-08 17:24       ` Derrick Stolee
2023-08-09 10:03         ` Phillip Wood
2023-08-08 12:08   ` Phillip Wood
2023-08-08 17:06     ` Derrick Stolee
2023-08-08 17:14       ` Derrick Stolee
2023-08-09 10:00         ` Phillip Wood
2023-08-10 20:39 ` [PATCH v2 0/8] maintenance: schedule maintenance on a random minute Derrick Stolee via GitGitGadget
2023-08-10 20:39   ` [PATCH v2 1/8] maintenance: add get_random_minute() Derrick Stolee via GitGitGadget
2023-08-10 21:25     ` Taylor Blau
2023-08-10 20:39   ` [PATCH v2 2/8] maintenance: use random minute in launchctl scheduler Derrick Stolee via GitGitGadget
2023-08-10 20:39   ` [PATCH v2 3/8] maintenance: use random minute in Windows scheduler Derrick Stolee via GitGitGadget
2023-08-10 20:39   ` [PATCH v2 4/8] maintenance: use random minute in cron scheduler Derrick Stolee via GitGitGadget
2023-08-10 20:39   ` [PATCH v2 5/8] maintenance: swap method locations Derrick Stolee via GitGitGadget
2023-08-10 20:39   ` [PATCH v2 6/8] maintenance: use random minute in systemd scheduler Derrick Stolee via GitGitGadget
2023-08-14 11:26     ` Phillip Wood
2023-08-10 20:39   ` [PATCH v2 7/8] maintenance: fix systemd schedule overlaps Derrick Stolee via GitGitGadget
2023-08-10 21:22     ` Junio C Hamano
2023-08-14 11:27     ` Phillip Wood
2023-08-10 20:39   ` [PATCH v2 8/8] maintenance: update schedule before config Derrick Stolee via GitGitGadget
2023-08-14 11:28     ` Phillip Wood

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=ZNJVycLBUIXqZ0Xb@nand.local \
    --to=me@ttaylorr.com \
    --cc=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=lenaic@lhuard.fr \
    --cc=sandals@crustytoothpaste.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.