From patchwork Thu May 4 04:06:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Cruz X-Patchwork-Id: 1776697 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=sMbcYqq5; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QBgHx6CSkz20fg for ; Thu, 4 May 2023 14:07:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AC0F93857359 for ; Thu, 4 May 2023 04:07:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC0F93857359 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683173226; bh=IiGwGH54P8Kim2f3r8WX1AcLsr1hRMDRkFkacfjO2IA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sMbcYqq5pdFNo/P3edi7RAhjqkqC2cBY/EZzD9U2PBgaYqbvtr3v1SnqhVqqubsl/ G2y875DnKnvAi2c1o/n9ovzpqwvsC7Eiev0D/0wXcZmz62iADkXPuKODrI7Yii0vUj EBeN181OAdb2j3G0+vhTwu/0z+8eii9RHU6zDOuo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x1132.google.com (mail-yw1-x1132.google.com [IPv6:2607:f8b0:4864:20::1132]) by sourceware.org (Postfix) with ESMTPS id 605763858D28 for ; Thu, 4 May 2023 04:06:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 605763858D28 Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-559de1d36a9so79204717b3.1 for ; Wed, 03 May 2023 21:06:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683173209; x=1685765209; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IiGwGH54P8Kim2f3r8WX1AcLsr1hRMDRkFkacfjO2IA=; b=KbXJryjSe5X0qY5+8GEURicwY/O3Vnz0etD4smCQSoAYZ7MVXlb/p0DibnFWaOn/r9 BRqwScQS5tpA80j43fsOcp6IH04ypjBieMhLjPjFSshvDMksCJKsjqDuSYuuF4BlgWz+ seGZLtvNIUEFehXoS0RNxZoF000waeNRgwhFY56+PxTcsp0uvRD6h1YnXO9FCwJ2lcxc kkVCzWoCTDrYZLMBymoErPnc4x9Rd0uAwJd83iLrCp86+/FG6Q3Jr58mBHJgykStFzoy QX8ZRn4DBj9Q+DpQOVxWuI3MDWK/BfECGu7x5SgUVqpypXZ5UDCLt8rAUxJ1A8rU4hP5 LRgg== X-Gm-Message-State: AC+VfDyHJL2J473fh5hEoxN6ufadVCdpgclI4U0PXT+CrJxqrivAg2mC 7dTzjoNNdU6POTmsh4nKupVIFyHUbg== X-Google-Smtp-Source: ACHHUZ5ICR0VwINdv5QI/h9ZLwCp3nxMSt1OREb5tfEf9fUE6UM653eam5HlSUSLhYFi04wVjBLglg== X-Received: by 2002:a0d:d548:0:b0:559:de1d:3253 with SMTP id x69-20020a0dd548000000b00559de1d3253mr1091822ywd.21.1683173209278; Wed, 03 May 2023 21:06:49 -0700 (PDT) Received: from jupiter.tail36e24.ts.net (dynamic-acs-24-154-253-225.zoominternet.net. [24.154.253.225]) by smtp.gmail.com with ESMTPSA id r127-20020a0dcf85000000b0054f0baa196asm9048584ywd.107.2023.05.03.21.06.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 21:06:48 -0700 (PDT) Date: Thu, 4 May 2023 00:06:46 -0400 To: bug-hurd@gnu.org, libc-alpha@sourceware.org Subject: [PATCH glibc] Update sysdeps/mach/hurd/ioctl.c to make it more portable Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , X-Patchwork-Original-From: Flavio Cruz via Libc-alpha From: Flavio Cruz Reply-To: Flavio Cruz Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Summary of the changes: - Update msg_align to use ALIGN_UP like we have done in previous patches. Use it below whenever necessary to avoid repeating the same alignment logic. - Define BAD_TYPECHECK to make it easier to do type checking in a few places below. - Update io2mach_type to use designated initializers. - Make RetCodeType use mach_msg_type_t. mach_msg_type_t is 8 byte for x86_64, so this make it portable. - Also call msg_align for _IOT_COUNT2/_IOT_TYPE2 since it is more correct. --- sysdeps/mach/hurd/ioctl.c | 47 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/sysdeps/mach/hurd/ioctl.c b/sysdeps/mach/hurd/ioctl.c index ab913a5943..66daaa751e 100644 --- a/sysdeps/mach/hurd/ioctl.c +++ b/sysdeps/mach/hurd/ioctl.c @@ -16,6 +16,7 @@ . */ #include +#include #include #include #include @@ -31,29 +32,40 @@ #include +#define msg_align(x) ALIGN_UP (x, __alignof__ (uintptr_t)) #define typesize(type) (1 << (type)) +/* Macro used by MIG to cleanly check the type. */ +#define BAD_TYPECHECK(type, check) __glibc_unlikely (({ \ + union { mach_msg_type_t t; uint32_t w; } _t, _c; \ + _t.t = *(type); _c.t = *(check);_t.w != _c.w; })) /* Perform the I/O control operation specified by REQUEST on FD. The actual type and use of ARG and the return value depend on REQUEST. */ int __ioctl (int fd, unsigned long int request, ...) { -#ifdef MACH_MSG_TYPE_CHAR +#ifdef MACH_MSG_TYPE_BIT /* Map individual type fields to Mach IPC types. */ static const int mach_types[] = { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32, MACH_MSG_TYPE_INTEGER_64 }; -#define io2mach_type(count, type) \ - ((mach_msg_type_t) { mach_types[type], typesize (type) * 8, count, 1, 0, 0 }) +#define io2mach_type(count, type) \ + ((mach_msg_type_t) { \ + .msgt_name = mach_types[type], \ + .msgt_size = typesize(type) * 8, \ + .msgt_number = count, \ + .msgt_inline = TRUE, \ + .msgt_longform = FALSE, \ + .msgt_deallocate = FALSE, \ + .msgt_unused = 0 \ + }) #endif /* Extract the type information encoded in the request. */ unsigned int type = _IOC_TYPE (request); /* Message buffer. */ -#define msg_align(x) \ - (((x) + sizeof (mach_msg_type_t) - 1) & ~(sizeof (mach_msg_type_t) - 1)) struct { #ifdef MACH_MSG_TYPE_BIT @@ -63,14 +75,14 @@ __ioctl (int fd, unsigned long int request, ...) struct { mach_msg_header_t Head; - int RetCodeType; + mach_msg_type_t RetCodeType; kern_return_t RetCode; } header_typecheck; }; char data[3 * sizeof (mach_msg_type_t) + msg_align (_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type))) + msg_align (_IOT_COUNT1 (type) * typesize (_IOT_TYPE1 (type))) - + _IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type))]; + + msg_align (_IOT_COUNT2 (type) * typesize (_IOT_TYPE2 (type)))]; #else /* Untyped Mach IPC format. */ mig_reply_error_t header; char data[_IOT_COUNT0 (type) * typesize (_IOT_TYPE0 (type)) @@ -128,8 +140,7 @@ __ioctl (int fd, unsigned long int request, ...) void *p = &t[1]; *t = io2mach_type (count, type); p = __mempcpy (p, argptr, len); - p = (void *) (((uintptr_t) p + sizeof (*t) - 1) - & ~(sizeof (*t) - 1)); + p = (void *) msg_align ((uintptr_t) p); t = p; #else p = __mempcpy (p, argptr, len); @@ -150,7 +161,7 @@ __ioctl (int fd, unsigned long int request, ...) #ifdef MACH_MSG_TYPE_BIT *t++ = io2mach_type (1, _IOTS (integer_t)); *(integer_t *) t = (integer_t) (intptr_t) arg; - t = (void *) t + sizeof (integer_t); + t = (void *) msg_align ((uintptr_t) t + sizeof (integer_t)); #else *(integer_t *) p = (integer_t) (intptr_t) arg; p = (void *) p + sizeof (integer_t); @@ -205,9 +216,8 @@ __ioctl (int fd, unsigned long int request, ...) return MIG_TYPE_ERROR; #ifdef MACH_MSG_TYPE_BIT - if (msg.header_typecheck.RetCodeType - != ((union { mach_msg_type_t t; int i; }) - { t: io2mach_type (1, _IOTS (msg.header.RetCode)) }).i) + mach_msg_type_t ipctype = io2mach_type(1, _IOTS (msg.header.RetCode)); + if (BAD_TYPECHECK (&msg.header_typecheck.RetCodeType, &ipctype)) return MIG_TYPE_ERROR; #endif return msg.header.RetCode; @@ -259,8 +269,7 @@ __ioctl (int fd, unsigned long int request, ...) /* Add the size of the type and data. */ reply_size += sizeof (mach_msg_type_t) + typesize (type) * count; /* Align it to word size. */ - reply_size += sizeof (mach_msg_type_t) - 1; - reply_size &= ~(sizeof (mach_msg_type_t) - 1); + reply_size = msg_align (reply_size); #else reply_size += typesize (type) * count; #endif @@ -299,16 +308,14 @@ __ioctl (int fd, unsigned long int request, ...) { const size_t len = count * typesize (type); #ifdef MACH_MSG_TYPE_BIT - union { mach_msg_type_t t; int i; } ipctype; - ipctype.t = io2mach_type (count, type); - if (*(int *) t != ipctype.i) + const mach_msg_type_t ipctype = io2mach_type(count, type); + if (BAD_TYPECHECK (t, &ipctype)) return 1; ++t; memcpy (store, t, len); if (update != NULL) *update += len; - t = (void *) (((uintptr_t) t + len + sizeof (*t) - 1) - & ~(sizeof (*t) - 1)); + t = (mach_msg_type_t *) msg_align ((uintptr_t) t + len); #else memcpy (store, p, len); p += len;