From patchwork Fri Dec 13 02:25:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 1208938 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-108039-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="KhVS/IrF"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47YvfW0rvGz9sPL for ; Fri, 13 Dec 2019 13:25:54 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=pIz4S5dZCvJ/vzFjBuW+NEIuzvFhwekCA13wCRXuIWsGDJboYYuqM fr4yATuFb+oLvbYLdjN5lX3Ho1prbJT+OXvIfB4X+gBxFmciRapFHRL3djtQL42S bxmyJ9usxZD0hmKpI6fwzQCzFItsdZ/7Z73AED0Ji25C4QWWTK7cVM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=IHHEMF3XBiAk8m2DITLI/hnzJyk=; b=KhVS/IrFQbIG06QqHF3Ji55GUDr/ e6PQ2sMhTVfvJhQIrZsHRStuYaXN5/+0YbYTlReS6HRe13cNv1MqyXGzlAroayXV MzSpgXalqjk4Xec547HetOudS4qKRUNzFebiENobkRmgDq8zahrXNEffbX3zE0ev /kutlv+K+XNhRHs= Received: (qmail 123896 invoked by alias); 13 Dec 2019 02:25:30 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123813 invoked by uid 89); 13 Dec 2019 02:25:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=drepper, Drepper, HContent-Transfer-Encoding:8bit X-HELO: hera.aquilenet.fr From: Samuel Thibault To: libc-alpha@sourceware.org Cc: Samuel Thibault , commit-hurd@gnu.org Subject: [hurd,commited 1/2] hurd: Implement __close_nocancel_nostatus Date: Fri, 13 Dec 2019 03:25:18 +0100 Message-Id: <20191213022519.164161-3-samuel.thibault@ens-lyon.org> In-Reply-To: <20191213022519.164161-1-samuel.thibault@ens-lyon.org> References: <20191213022519.164161-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 * sysdeps/mach/hurd/not-cancel.h: New file. --- sysdeps/mach/hurd/not-cancel.h | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 sysdeps/mach/hurd/not-cancel.h diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h new file mode 100644 index 0000000000..b78870a5b7 --- /dev/null +++ b/sysdeps/mach/hurd/not-cancel.h @@ -0,0 +1,62 @@ +/* Uncancelable versions of cancelable interfaces. Hurd version. + Copyright (C) 2003-2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2003. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef NOT_CANCEL_H +# define NOT_CANCEL_H + +#include +#include +#include +#include +#include +#include +#include + +/* For now we have none. Map the name to the normal functions. */ +#define __open_nocancel(...) \ + __open (__VA_ARGS__) +#define __open64_nocancel(...) \ + __open64 (__VA_ARGS__) +#define __openat_nocancel(...) \ + __openat (__VA_ARGS__) +#define __openat64_nocancel(...) \ + __openat64 (__VA_ARGS__) +#define __close_nocancel(fd) \ + __close (fd) + +static inline void +__close_nocancel_nostatus (int fd) +{ + HURD_FD_USE (fd, _hurd_fd_close (descriptor)); +} + +#define __read_nocancel(fd, buf, n) \ + __read (fd, buf, n) +#define __pread64_nocancel(fd, buf, count, offset) \ + __pread64 (fd, buf, count, offset) +#define __write_nocancel(fd, buf, n) \ + __write (fd, buf, n) +#define __writev_nocancel_nostatus(fd, iov, n) \ + (void) __writev (fd, iov, n) +# define __waitpid_nocancel(pid, stat_loc, options) \ + __waitpid (pid, stat_loc, options) +#define __fcntl64_nocancel(fd, cmd, ...) \ + __fcntl64 (fd, cmd, __VA_ARGS__) + +#endif /* NOT_CANCEL_H */ From patchwork Fri Dec 13 02:25:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 1208939 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-108040-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="ItnP+4Kb"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47Yvfh1CXGz9sPL for ; Fri, 13 Dec 2019 13:26:03 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=N2c5J6vHQt48L/jG77yFSF4jFIRpm4EdDDQVRKyJFHu3OAhLoSlix fuRcnp8ZcBUASLbei9V7IWTNuNRhF5FAWNBsAPP4zz83daDhhr+tRKIebYXICsrH y/FHsdNPeOB4B+rBLuOSbd45ovYZYlt0LwBop7uZY5yq/wV3KT1kT8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=YbEArzypjpbuMXSwRrcgPrjBZRY=; b=ItnP+4Kb4P7ADC+rhVS8xksZOxsq p2qUHTAiJgEOcwWTa1vIs2pS3vSLlBzO2I/vLeFJKp0LVFZtfxYA2bXk7RmxyUDh xG80Ztd8/xCITejXT6s9CjvWOjayw5emTZY6PInwmFwcQ2RKqJ8nCeh+GlYo1HY8 kfhLm4yDMVoAF1g= Received: (qmail 123929 invoked by alias); 13 Dec 2019 02:25:30 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123824 invoked by uid 89); 13 Dec 2019 02:25:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=ssize_t, lengths, boot X-HELO: hera.aquilenet.fr From: Samuel Thibault To: libc-alpha@sourceware.org Cc: Andrew Eggenberger , commit-hurd@gnu.org Subject: [hurd, commited 2/2] hurd: add getrandom and getentropy implementations Date: Fri, 13 Dec 2019 03:25:19 +0100 Message-Id: <20191213022519.164161-4-samuel.thibault@ens-lyon.org> In-Reply-To: <20191213022519.164161-1-samuel.thibault@ens-lyon.org> References: <20191213022519.164161-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 From: Andrew Eggenberger * sysdeps/mach/hurd/getentropy.c: New file. * sysdeps/mach/hurd/getrandom.c: Likewise. --- sysdeps/mach/hurd/getentropy.c | 64 ++++++++++++++++++++++++++++++++++ sysdeps/mach/hurd/getrandom.c | 43 +++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 sysdeps/mach/hurd/getentropy.c create mode 100644 sysdeps/mach/hurd/getrandom.c diff --git a/sysdeps/mach/hurd/getentropy.c b/sysdeps/mach/hurd/getentropy.c new file mode 100644 index 0000000000..d55eec8984 --- /dev/null +++ b/sysdeps/mach/hurd/getentropy.c @@ -0,0 +1,64 @@ +/* Implementation of getentropy based on getrandom. + Copyright (C) 2016-2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +/* Write LENGTH bytes of randomness starting at BUFFER. Return 0 on + success and -1 on failure. */ +int +getentropy (void *buffer, size_t length) +{ + /* The interface is documented to return EIO for buffer lengths + longer than 256 bytes. */ + if (length > 256) + { + __set_errno (EIO); + return -1; + } + + /* Try to fill the buffer completely. Even with the 256 byte limit + above, we might still receive an EINTR error (when blocking + during boot). */ + void *end = buffer + length; + while (buffer < end) + { + /* NB: No cancellation point. */ + ssize_t bytes = getrandom (buffer, end - buffer, 0); + if (bytes < 0) + { + if (errno == EINTR) + /* Try again if interrupted by a signal. */ + continue; + else + return -1; + } + if (bytes == 0) + { + /* No more bytes available. This should not happen under + normal circumstances. */ + __set_errno (EIO); + return -1; + } + /* Try again in case of a short read. */ + buffer += bytes; + } + return 0; +} diff --git a/sysdeps/mach/hurd/getrandom.c b/sysdeps/mach/hurd/getrandom.c new file mode 100644 index 0000000000..0147aeef3a --- /dev/null +++ b/sysdeps/mach/hurd/getrandom.c @@ -0,0 +1,43 @@ +/* Hurdish implementation of getrandom + Copyright (C) 2016-2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +/* Write up to LENGTH bytes of randomness starting at BUFFER. + Return the number of bytes written, or -1 on error. */ +ssize_t +getrandom (void *buffer, size_t length, unsigned int flags) +{ + const char *random_source = "/dev/urandom"; + size_t amount_read; + int fd; + + if (flags & GRND_RANDOM) + random_source = "/dev/random"; + + fd = __open_nocancel(random_source, O_RDONLY | O_CLOEXEC); + if (fd == -1) + return -1; + + amount_read = __read_nocancel(fd, buffer, length); + __close_nocancel_nostatus(fd); + return amount_read; +}