From patchwork Mon Oct 30 21:32:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 739300 Delivered-To: patch@linaro.org Received: by 2002:a5d:4c47:0:b0:32d:baff:b0ca with SMTP id n7csp1341750wrt; Mon, 30 Oct 2023 14:34:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZGxKF/yqrclacAU5PmDpcZydGTBfJGlxvr/SmYA/esdXh8P9OSLCYsD0zR5KyJLbodnCe X-Received: by 2002:a05:620a:2886:b0:774:2470:6797 with SMTP id j6-20020a05620a288600b0077424706797mr1524959qkp.21.1698701639993; Mon, 30 Oct 2023 14:33:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698701639; cv=pass; d=google.com; s=arc-20160816; b=IM/OfRybcqO45ObXXXd6DI6FafT5owbZgLj2rlD1G17sZT8mMegQB/ROlq0Vz0KOOw IDV6hblEc2rfxpYa7rgNgdlUaCHSlv7HGAOE8A/CeoAWZUk6cSyuSy2+yeNLVPKb/2Ok OtTMl+FlZa+UHf1yau8EnMmYh8DdW7IRNGCrO+QvRDs8oyxYuHYnPmp2KeIQX7Oshfc9 RoSSlWH/UDFDU3T/4rJ0LpxKnWoGE12X3gJ3I8aHcIsFX7AHtihuoVYvIVgThu0EoRf2 KG+o02RLV/qBumVy8IapeCeXpwL2K5gQ2CDh6Mx5yW8QP/VcL5mBaPmLtEIjVox4DV9Z nguQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=b4YVPJTrB8luKqYTJ6F8FHHBi3HA/0MWDUxg/HLMbXs=; fh=x+/sfya+TES1qlBiDsEdYFy6khw7IV+tu17R0mskoJ0=; b=FG2tmxGGyQUpoEUtu3yf4JBDI27eSndPHz8H9F/hlb8SscmVwYpKuBAP1jAQwLuWxD tXQgTMRNP7xexcJq5pAMRiFKsxX6J8guZ/sX4sTON1urds/BYZlBzV5MuJZiQfLvI811 UOTVuzwy7996eRC46hKyz12wpnl5WALE7p8JxOXzWqD6GRfOQ2XlqsKe84/u/ye2eQ2P BGiTErpfxrLzvqeG0BWFYve9HRf2XIOrMWOKfRS9+M1FUYJwLDcmzNAXeGpnLPWB8zWY 5OrYOuOtGRKmz+r8MZ2pGGOUC+ai5VwuaikPZxyZed20Qx493M836UgpzTDU18/W8Fzc lNdg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A2Ox84DM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o12-20020a05620a22cc00b00779d0155329si6122076qki.702.2023.10.30.14.33.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 14:33:59 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A2Ox84DM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A70E73858433 for ; Mon, 30 Oct 2023 21:33:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by sourceware.org (Postfix) with ESMTPS id 9E5EE3858CDB for ; Mon, 30 Oct 2023 21:33:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E5EE3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9E5EE3858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698701583; cv=none; b=FPXZvjL2kUTj3VS1JjZbYRaUk8NhUvGrq29xm0bFWY/fgE6r2LhjvckpynBTDEbpoqdawxZWwXqN+wdvLzc/rNN9j+Ag+VRkISKoMjRhjTTyX/xhe7ktPxSOesAY2hNzlSz+aTUVIlb9tpC1jGJNQp4sZ7DT16SGwVeiu4YnGqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698701583; c=relaxed/simple; bh=N7sKRZK8RJETe+Yo4ejNJ5EfCrwwFxHl4dRhTRQ2cGw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=X7dD7JSr4o3PEHyWe3Ri/FlmwTVpx/IVr7AnPLhH8iD8gLP6nK/Jo/K9++jAn3bdJkgSkRlXVeey9lWtb2Y5O0L0DTB9mOOfYz9jVS5uu/Pgfm2yHv4NTADjyxyyEMh5JQqHaimwfHg0XO82hYtak0lGgGX/AJ6spbytDjSLMTM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-5a8628e54d4so44413807b3.0 for ; Mon, 30 Oct 2023 14:33:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698701579; x=1699306379; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=b4YVPJTrB8luKqYTJ6F8FHHBi3HA/0MWDUxg/HLMbXs=; b=A2Ox84DMsc4XaH4VOQpd3AoTP7ZOlS+ZuBNSRW0hmoiW3OURflynZIBU4MpUr5XPD2 iY63w7dO2uddPPue6f/WYFfmkEO7f9Tx5vvfMej/9d8QkabJLnyVcMifYLpoN/3XY+xi Txazu/EdSm0rbxuwD1Q1bsppnlI2g9wmX2Q7Gb4HhnvKlZS//H3OaKzhoXFb/4XRNak1 ijU/ssO5PbtmNMqq3w1D4zN11G84R6ZGJnKvRp5B4ohq8niyMj/MJYM4/0oI2L1mOfff ap+nddDdV9MbhT9J70nozIEyknJSLcsahTliL9graH8ddk3WvwOOJN3lDso7avlQb5R5 g4GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698701579; x=1699306379; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b4YVPJTrB8luKqYTJ6F8FHHBi3HA/0MWDUxg/HLMbXs=; b=LDAF84emMJ8LvIlbiRuzxwhtQqEYHl4pqS3QISYPHH6DSZqeRh4o+m0U/Dd+eH9Kzy x/D8yCgBZfle5IaC4Byt02hwLlaPIXtu/I9C8uUP85L+7xLeZtIKIHXzAzy16VSy+fIB LDbUYOAEF8wWkPXIFBx6JKrHeJzWYLSEkOsMzSuR4huxWGnY3o0RVYN+tPxzVPiGxd7T FGCFVa2E7jiIVLQqJTh+CVKd54A5C6l8NnUrQ3cgfNm1MEDN0QiD56B9XHudy4D0GigD D5FVNJhW/QMZWX9X7nx5zP8DnjvY1Ic1qK44A+dpyWqcYSKTczHGIo09vUiOkjjkOTw2 Lz/w== X-Gm-Message-State: AOJu0Yw9bOxCg1vXTrwv8iYZPh0HXbOXcqZZApMuV3C+sYy0kmJVA1Nf GNeYZNVsuD/PJFnAouFEdRTyNlLVGx4aw9Q/2gC39w== X-Received: by 2002:a05:690c:15:b0:5ae:15bb:466a with SMTP id bc21-20020a05690c001500b005ae15bb466amr1004940ywb.14.1698701579348; Mon, 30 Oct 2023 14:32:59 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:3d3c:883c:5760:462b:b60b]) by smtp.gmail.com with ESMTPSA id x11-20020a817c0b000000b005a0f9718a5fsm178739ywc.78.2023.10.30.14.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 14:32:58 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Joseph Myers Subject: [PATCH 6/6] linux: Use fchmodat2 on fchmod for flags different than 0 (BZ 26401) Date: Mon, 30 Oct 2023 18:32:45 -0300 Message-Id: <20231030213245.2626895-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231030213245.2626895-1-adhemerval.zanella@linaro.org> References: <20231030213245.2626895-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org Linux 6.6 (09da082b07bbae1c) added support for fchmodat2, which is has similar semantic of fchmodat with an extra flag argument. This allow fchmodat to implement AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH without the need to procfs. The syscall is registered on all architectures (with value of 452 except on alpha which is 562, commit 78252deb023cf087). Checked on x86_64-linux-gnu on a 6.6 kernel. PS: setting it as RFC because there is no Linux 6.6 release yet. --- io/tst-lchmod.c | 4 +- sysdeps/unix/sysv/linux/fchmodat.c | 120 ++++++++++++---------- sysdeps/unix/sysv/linux/kernel-features.h | 8 ++ 3 files changed, 77 insertions(+), 55 deletions(-) diff --git a/io/tst-lchmod.c b/io/tst-lchmod.c index 2bf4835b05..6496dc61e0 100644 --- a/io/tst-lchmod.c +++ b/io/tst-lchmod.c @@ -219,9 +219,9 @@ test_1 (bool do_relative_path, int (*chmod_func) (int fd, const char *, mode_t, /* The error code from the openat fallback leaks out. */ if (errno != ENFILE && errno != EMFILE) TEST_COMPARE (errno, EOPNOTSUPP); + xstat (path_file, &st); + TEST_COMPARE (st.st_mode & 0777, 3); } - xstat (path_file, &st); - TEST_COMPARE (st.st_mode & 0777, 3); /* Close the descriptors. */ for (int *pfd = fd_list_begin (&fd_list); pfd < fd_list_end (&fd_list); diff --git a/sysdeps/unix/sysv/linux/fchmodat.c b/sysdeps/unix/sysv/linux/fchmodat.c index 99527a3727..99d3df6440 100644 --- a/sysdeps/unix/sysv/linux/fchmodat.c +++ b/sysdeps/unix/sysv/linux/fchmodat.c @@ -26,66 +26,80 @@ #include #include -int -fchmodat (int fd, const char *file, mode_t mode, int flag) +#if !__ASSUME_FCHMODAT2 +static int +fchmodat_fallback (int fd, const char *file, mode_t mode, int flag) { - if (flag == 0) - return INLINE_SYSCALL (fchmodat, 3, fd, file, mode); - else if (flag != AT_SYMLINK_NOFOLLOW) + if (flag != AT_SYMLINK_NOFOLLOW) return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); - else - { - /* The kernel system call does not have a mode argument. - However, we can create an O_PATH descriptor and change that - via /proc (which does not resolve symbolic links). */ - int pathfd = __openat_nocancel (fd, file, - O_PATH | O_NOFOLLOW | O_CLOEXEC); - if (pathfd < 0) - /* This may report errors such as ENFILE and EMFILE. The - caller can treat them as temporary if necessary. */ - return pathfd; + /* The kernel system call does not have a mode argument. + However, we can create an O_PATH descriptor and change that + via /proc (which does not resolve symbolic links). */ - /* Use fstatat because fstat does not work on O_PATH descriptors - before Linux 3.6. */ - struct __stat64_t64 st; - if (__fstatat64_time64 (pathfd, "", &st, AT_EMPTY_PATH) != 0) - { - __close_nocancel (pathfd); - return -1; - } + int pathfd = __openat_nocancel (fd, file, + O_PATH | O_NOFOLLOW | O_CLOEXEC); + if (pathfd < 0) + /* This may report errors such as ENFILE and EMFILE. The + caller can treat them as temporary if necessary. */ + return pathfd; - /* Some Linux versions with some file systems can actually - change symbolic link permissions via /proc, but this is not - intentional, and it gives inconsistent results (e.g., error - return despite mode change). The expected behavior is that - symbolic link modes cannot be changed at all, and this check - enforces that. */ - if (S_ISLNK (st.st_mode)) - { - __close_nocancel (pathfd); - __set_errno (EOPNOTSUPP); - return -1; - } + /* Use fstatat because fstat does not work on O_PATH descriptors + before Linux 3.6. */ + struct __stat64_t64 st; + if (__fstatat64_time64 (pathfd, "", &st, AT_EMPTY_PATH) != 0) + { + __close_nocancel (pathfd); + return -1; + } - /* For most file systems, fchmod does not operate on O_PATH - descriptors, so go through /proc. */ - struct fd_to_filename filename; - int ret = __chmod (__fd_to_filename (pathfd, &filename), mode); - if (ret != 0) - { - if (errno == ENOENT) - /* /proc has not been mounted. Without /proc, there is no - way to upgrade the O_PATH descriptor to a full - descriptor. It is also not possible to re-open the - file without O_PATH because the file name may refer to - another file, and opening that without O_PATH may have - side effects (such as blocking, device rewinding, or - releasing POSIX locks). */ - __set_errno (EOPNOTSUPP); - } + /* Some Linux versions with some file systems can actually + change symbolic link permissions via /proc, but this is not + intentional, and it gives inconsistent results (e.g., error + return despite mode change). The expected behavior is that + symbolic link modes cannot be changed at all, and this check + enforces that. */ + if (S_ISLNK (st.st_mode)) + { __close_nocancel (pathfd); - return ret; + __set_errno (EOPNOTSUPP); + return -1; + } + + /* For most file systems, fchmod does not operate on O_PATH + descriptors, so go through /proc. */ + struct fd_to_filename filename; + int ret = __chmod (__fd_to_filename (pathfd, &filename), mode); + if (ret != 0) + { + if (errno == ENOENT) + /* /proc has not been mounted. Without /proc, there is no + way to upgrade the O_PATH descriptor to a full + descriptor. It is also not possible to re-open the + file without O_PATH because the file name may refer to + another file, and opening that without O_PATH may have + side effects (such as blocking, device rewinding, or + releasing POSIX locks). */ + __set_errno (EOPNOTSUPP); } + __close_nocancel (pathfd); + return ret; +} +#endif + +int +fchmodat (int fd, const char *file, mode_t mode, int flag) +{ +#if __ASSUME_FCHMODAT2 + return INLINE_SYSCALL_CALL (fchmodat2, fd, file, mode, flag); +#else + if (flag == 0) + return INLINE_SYSCALL_CALL (fchmodat, fd, file, mode); + + int r = INLINE_SYSCALL_CALL (fchmodat2, fd, file, mode, flag); + if (r != 0 && errno == ENOSYS) + return fchmodat_fallback (fd, file, mode, flag); + return r; +#endif } libc_hidden_def (fchmodat) diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 07b440f4ee..670d2604d2 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -252,4 +252,12 @@ # define __ASSUME_CLONE3 0 #endif +/* The fchmodat2 system call was introduced across all architectures + in Linux 6.6. */ +#if __LINUX_KERNEL_VERSION >= 0x060600 +# define __ASSUME_FCHMODAT2 1 +#else +# define __ASSUME_FCHMODAT2 0 +#endif + #endif /* kernel-features.h */