From: Junio C Hamano <gitster@pobox.com>
To: Taylor Blau <me@ttaylorr.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, 08 Aug 2023 09:34:21 -0700 [thread overview]
Message-ID: <xmqqbkfha3xu.fsf@gitster.g> (raw)
In-Reply-To: <ZNJVycLBUIXqZ0Xb@nand.local> (Taylor Blau's message of "Tue, 8 Aug 2023 10:48:41 -0400")
Taylor Blau <me@ttaylorr.com> writes:
> 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:
> ...
> - If a different function also calls `srand()`, the PRNG may start
> generating repeated values (if that caller also initialized the PRNG
> with `getpid()`).
Hmph, I didn't think about that one. I do not think it can be a
viable attack vector to attack _this_ code, but the other function
might be. But if the other function is worth attacking and
attackable, it ought to be using crypto-secure one and not srand(),
so this argument may not give us a good justification X-<.
Provided if code simplification is a good enough rationale, the
patch looks sensible, but I find its use of u64 a bit questionable
(though not wrong). I would have expected that the type of "rand"
would be the same as backoff_ms and wait_ms, two variables involved
in the same expression.
Thanks.
> 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
next prev parent reply other threads:[~2023-08-08 18:43 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
2023-08-08 16:34 ` Junio C Hamano [this message]
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=xmqqbkfha3xu.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=derrickstolee@github.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=lenaic@lhuard.fr \
--cc=me@ttaylorr.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).