From patchwork Fri Dec 2 22:45:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 61404 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3B9B5385B1A7 for ; Fri, 2 Dec 2022 22:45:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from sonata.ens-lyon.org (domu-toccata.ens-lyon.fr [140.77.166.138]) by sourceware.org (Postfix) with ESMTPS id 871E13857B8E for ; Fri, 2 Dec 2022 22:45:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 871E13857B8E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=bounce.ens-lyon.org Received: from localhost (localhost [127.0.0.1]) by sonata.ens-lyon.org (Postfix) with ESMTP id 8D5DB20148; Fri, 2 Dec 2022 23:45:29 +0100 (CET) Received: from sonata.ens-lyon.org ([127.0.0.1]) by localhost (sonata.ens-lyon.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Wi2ZfOMcwPAq; Fri, 2 Dec 2022 23:45:29 +0100 (CET) Received: from begin (lfbn-bor-1-376-208.w109-215.abo.wanadoo.fr [109.215.91.208]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by sonata.ens-lyon.org (Postfix) with ESMTPSA id 4D4C52013E; Fri, 2 Dec 2022 23:45:29 +0100 (CET) Received: from samy by begin with local (Exim 4.96) (envelope-from ) id 1p1Eme-00392Q-32; Fri, 02 Dec 2022 23:45:28 +0100 From: Samuel Thibault To: libc-alpha@sourceware.org Cc: Sergey Bugaev , commit-hurd@gnu.org Subject: [hurd,commited] hurd: Make getrandom cache the server port Date: Fri, 2 Dec 2022 23:45:28 +0100 Message-Id: <20221202224528.749596-1-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Sergey Bugaev Previously, getrandom would, each time it's called, traverse the file system to find /dev/urandom, fetch some random data from it, then throw away that port. This is quite slow, while calls to getrandom are genrally expected to be fast. Additionally, this means that getrandom can not work when /dev/urandom is unavailable, such as inside a chroot that lacks one. User programs expect calls to getrandom to work inside a chroot if they first call getrandom outside of the chroot. In particular, this is known to break the OpenSSH server, and in that case the issue is exacerbated by the API of arc4random, which prevents it from properly reporting errors, forcing glibc to abort on failure. This causes sshd to just die once it tries to generate a random number. Caching the random server port, in a manner similar to how socket server ports are cached, both improves the performance and works around the chroot issue. Tested on i686-gnu with the following program: pthread_barrier_t barrier; void *worker(void*) { pthread_barrier_wait(&barrier); uint32_t sum = 0; for (int i = 0; i < 10000; i++) { sum += arc4random(); } return (void *)(uintptr_t) sum; } int main() { pthread_t threads[THREAD_COUNT]; pthread_barrier_init(&barrier, NULL, THREAD_COUNT); for (int i = 0; i < THREAD_COUNT; i++) { pthread_create(&threads[i], NULL, worker, NULL); } for (int i = 0; i < THREAD_COUNT; i++) { void *retval; pthread_join(threads[i], &retval); printf("Thread %i: %lu\n", i, (unsigned long)(uintptr_t) retval); } In my totally unscientific benchmark, with this patch, this completes in about 7 seconds, whereas previously it took about 50 seconds. This program was also used to test that getrandom () doesn't explode if the random server dies, but instead reopens the /dev/urandom anew. I have also verified that with this patch, OpenSSH can once again accept connections properly. Signed-off-by: Sergey Bugaev Message-Id: <20221202135558.23781-1-bugaevc@gmail.com> --- sysdeps/mach/hurd/getrandom.c | 117 +++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 15 deletions(-) diff --git a/sysdeps/mach/hurd/getrandom.c b/sysdeps/mach/hurd/getrandom.c index ad2d3ba387..9ee3ef74fb 100644 --- a/sysdeps/mach/hurd/getrandom.c +++ b/sysdeps/mach/hurd/getrandom.c @@ -16,10 +16,13 @@ License along with the GNU C Library; if not, see . */ +#include #include #include -#include -#include + +__libc_rwlock_define_initialized (static, lock); +static file_t random_server, random_server_nonblock, + urandom_server, urandom_server_nonblock; extern char *__trivfs_server_name __attribute__((weak)); @@ -29,9 +32,36 @@ ssize_t __getrandom (void *buffer, size_t length, unsigned int flags) { const char *random_source = "/dev/urandom"; - int open_flags = O_RDONLY | O_CLOEXEC; - size_t amount_read; - int fd; + int open_flags = O_RDONLY; + file_t server, *cached_server; + error_t err; + char *data = buffer; + mach_msg_type_number_t nread = length; + + switch (flags) + { + case 0: + cached_server = &urandom_server; + break; + case GRND_RANDOM: + cached_server = &random_server; + break; + case GRND_NONBLOCK: + cached_server = &urandom_server_nonblock; + break; + case GRND_RANDOM | GRND_NONBLOCK: + cached_server = &random_server_nonblock; + break; + default: + return __hurd_fail (EINVAL); + } + + if (flags & GRND_RANDOM) + random_source = "/dev/random"; + if (flags & GRND_NONBLOCK) + open_flags |= O_NONBLOCK; + /* No point in passing either O_NOCTTY, O_IGNORE_CTTY, or O_CLOEXEC + to file_name_lookup, since we're not making an fd. */ if (&__trivfs_server_name && __trivfs_server_name && __trivfs_server_name[0] == 'r' @@ -44,19 +74,76 @@ __getrandom (void *buffer, size_t length, unsigned int flags) /* We are random, don't try to read ourselves! */ return length; - if (flags & GRND_RANDOM) - random_source = "/dev/random"; +again: + __libc_rwlock_rdlock (lock); + server = *cached_server; + if (MACH_PORT_VALID (server)) + /* Attempt to read some random data using this port. */ + err = __io_read (server, &data, &nread, -1, length); + else + err = MACH_SEND_INVALID_DEST; + __libc_rwlock_unlock (lock); - if (flags & GRND_NONBLOCK) - open_flags |= O_NONBLOCK; + if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED) + { + file_t oldserver = server; + mach_port_urefs_t urefs; + + /* Slow path: the cached port didn't work, or there was no + cached port in the first place. */ + + __libc_rwlock_wrlock (lock); + server = *cached_server; + if (server != oldserver) + { + /* Someone else must have refetched the port while we were + waiting for the lock. */ + __libc_rwlock_unlock (lock); + goto again; + } + + if (MACH_PORT_VALID (server)) + { + /* It could be that someone else has refetched the port and + it got the very same name. So check whether it is a send + right (and not a dead name). */ + err = __mach_port_get_refs (__mach_task_self (), server, + MACH_PORT_RIGHT_SEND, &urefs); + if (!err && urefs > 0) + { + __libc_rwlock_unlock (lock); + goto again; + } + + /* Now we're sure that it's dead. */ + __mach_port_deallocate (__mach_task_self (), server); + } + + server = *cached_server = __file_name_lookup (random_source, + open_flags, 0); + __libc_rwlock_unlock (lock); + if (!MACH_PORT_VALID (server)) + /* No luck. */ + return -1; + + goto again; + } + + if (err) + return __hurd_fail (err); - fd = __open_nocancel(random_source, open_flags); - if (fd == -1) - return -1; + if (data != buffer) + { + if (nread > length) + { + __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread); + return __hurd_fail (EGRATUITOUS); + } + memcpy (buffer, data, nread); + __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread); + } - amount_read = __read_nocancel(fd, buffer, length); - __close_nocancel_nostatus(fd); - return amount_read; + return nread; } libc_hidden_def (__getrandom)