From patchwork Mon Sep 28 14:45:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1372668 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=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: 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=k28yRwkq; 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C0QLq2KtDz9sS8 for ; Tue, 29 Sep 2020 00:46:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 878D93953C05; Mon, 28 Sep 2020 14:46:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 878D93953C05 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1601304368; bh=1oYD86HxaEiwjHsTO/kq0ejui6wuT7Pv+hjNOGk8adU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=k28yRwkqODyV0F4kW6zT8Fcist0ZXdVRtIoF81P2fmPMhuSuIi0nEjt61c6UY0qO+ fCH2wB959MgPnUKtRZ9dp7pMyWjPSE1fRrWJUT1ZaMHY/R5N6fDrZafsJybVe155+u wfHvBPs8fIW5ZP/mPHZ6bytw1VTfXZKTDTSym8Zo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x844.google.com (mail-qt1-x844.google.com [IPv6:2607:f8b0:4864:20::844]) by sourceware.org (Postfix) with ESMTPS id E275B395383D for ; Mon, 28 Sep 2020 14:46:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E275B395383D Received: by mail-qt1-x844.google.com with SMTP id c18so916021qtw.5 for ; Mon, 28 Sep 2020 07:46:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1oYD86HxaEiwjHsTO/kq0ejui6wuT7Pv+hjNOGk8adU=; b=sqH4wiJi334bsyH8FYZ4Sa+XcWj1q2e3hOfF2jkdOwegqu0uf+/8YqGqDL+05lUpZr vvMCQWlwnynkojwr473Z6oUjGXnPAvyyrYzfzV75bti7REEzoo5RQ4JghIlekstff9VI a1nAnn+wZMVphW0BC63+lQn11JACCnI15q8Atl5srpkfT6ht8u/96tYZ0dQy5v02KoLL IzMGSn9HlSQ7Zw/T+vv2ruXe9Ci/JdQm2m+dBhLZgu2cWdqs1TvtqdbTriwmaQmR7z3N LSSW5jEU/QVbEVzO3nIyEAKxJXJsW60iuzgZzGnKgNeB3qTqdzNZZnMBB1m9va9bPlDC NVyA== X-Gm-Message-State: AOAM532dr0wZrAZZRGYbVuNKgZ/vVa8FU15ywW93id8v9IrBOz/K3wPv v861kY1QokNsPcA3Lv6kzIq38xUICng8ow== X-Google-Smtp-Source: ABdhPJzXEcGF+mFMI5Yw6gPDDGkUklogcSB9CnLPyFL2Xibz7i4W1aHwGK9j26el9mPzCJPh87DKMg== X-Received: by 2002:ac8:338d:: with SMTP id c13mr1949498qtb.5.1601304364128; Mon, 28 Sep 2020 07:46:04 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id j25sm1335782qtr.83.2020.09.28.07.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 07:46:03 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/6] sysvipc: Return EINVAL for invalid semctl commands Date: Mon, 28 Sep 2020 11:45:52 -0300 Message-Id: <20200928144556.239160-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200928144556.239160-1-adhemerval.zanella@linaro.org> References: <20200928144556.239160-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Robert O'Callahan , "Dmitry V . Levin" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" It avoids regressions on possible future commands that might require additional libc support. The downside is new commands added by newer kernels will need further glibc support. Checked on x86_64-linux-gnu and i686-linux-gnu (Linux v4.15 and v5.4). --- sysdeps/unix/sysv/linux/semctl.c | 10 +++ sysvipc/test-sysvipc.h | 135 +++++++++++++++++++++++++++++++ sysvipc/test-sysvsem.c | 5 ++ 3 files changed, 150 insertions(+) create mode 100644 sysvipc/test-sysvipc.h diff --git a/sysdeps/unix/sysv/linux/semctl.c b/sysdeps/unix/sysv/linux/semctl.c index 1cdabde8f2..dcff6e691d 100644 --- a/sysdeps/unix/sysv/linux/semctl.c +++ b/sysdeps/unix/sysv/linux/semctl.c @@ -158,6 +158,15 @@ __semctl64 (int semid, int semnum, int cmd, ...) arg64 = va_arg (ap, union semun64); va_end (ap); break; + case IPC_RMID: /* arg ignored. */ + case GETNCNT: + case GETPID: + case GETVAL: + case GETZCNT: + break; + default: + __set_errno (EINVAL); + return -1; } #if __IPC_TIME64 @@ -277,6 +286,7 @@ __semctl (int semid, int semnum, int cmd, ...) arg = va_arg (ap, union semun); va_end (ap); break; + /* __semctl64 handles non-supported commands. */ } struct __semid64_ds semid64; diff --git a/sysvipc/test-sysvipc.h b/sysvipc/test-sysvipc.h new file mode 100644 index 0000000000..86aa8df83d --- /dev/null +++ b/sysvipc/test-sysvipc.h @@ -0,0 +1,135 @@ +/* Basic definition for Sysv IPC test functions. + Copyright (C) 2020 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 + . */ + +#ifndef _TEST_SYSV_H +#define _TEST_SYSV_H + +#include +#include +#include +#include +#include + +/* Return the first invalid command SysV IPC command from common shared + between message queue, shared memory, and semaphore. */ +static inline int +first_common_invalid_cmd (void) +{ + const int common_cmds[] = { + IPC_RMID, + IPC_SET, + IPC_STAT, + IPC_INFO, + }; + + int invalid = 0; + for (int i = 0; i < array_length (common_cmds); i++) + { + if (invalid == common_cmds[i]) + { + invalid++; + i = 0; + } + } + + return invalid; +} + +/* Return the first invalid command SysV IPC command for message queue. */ +static inline int +first_msg_invalid_cmd (void) +{ + const int msg_cmds[] = { + MSG_STAT, + MSG_INFO, +#ifdef MSG_STAT_ANY + MSG_STAT_ANY, +#endif + }; + + int invalid = first_common_invalid_cmd (); + for (int i = 0; i < array_length (msg_cmds); i++) + { + if (invalid == msg_cmds[i]) + { + invalid++; + i = 0; + } + } + + return invalid; +} + +/* Return the first invalid command SysV IPC command for semaphore. */ +static inline int +first_sem_invalid_cmd (void) +{ + const int sem_cmds[] = { + GETPID, + GETVAL, + GETALL, + GETNCNT, + GETZCNT, + SETVAL, + SETALL, + SEM_STAT, + SEM_INFO, +#ifdef SEM_STAT_ANY + SEM_STAT_ANY, +#endif + }; + + int invalid = first_common_invalid_cmd (); + for (int i = 0; i < array_length (sem_cmds); i++) + { + if (invalid == sem_cmds[i]) + { + invalid++; + i = 0; + } + } + + return invalid; +} + +/* Return the first invalid command SysV IPC command for shared memory. */ +static inline int +first_shm_invalid_cmd (void) +{ + const int shm_cmds[] = { + SHM_STAT, + SHM_INFO, +#ifdef SHM_STAT_ANY + SHM_STAT_ANY, +#endif + }; + + int invalid = first_common_invalid_cmd (); + for (int i = 0; i < array_length (shm_cmds); i++) + { + if (invalid == shm_cmds[i]) + { + invalid++; + i = 0; + } + } + + return invalid; +} + +#endif /* _TEST_SYSV_H */ diff --git a/sysvipc/test-sysvsem.c b/sysvipc/test-sysvsem.c index b7284e0b48..61837d8f3d 100644 --- a/sysvipc/test-sysvsem.c +++ b/sysvipc/test-sysvsem.c @@ -25,6 +25,8 @@ #include #include +#include + #include #include #include @@ -80,6 +82,9 @@ do_test (void) FAIL_EXIT1 ("semget failed (errno=%d)", errno); } + TEST_COMPARE (semctl (semid, 0, first_sem_invalid_cmd (), NULL), -1); + TEST_COMPARE (errno, EINVAL); + /* Get semaphore kernel information and do some sanity checks. */ struct semid_ds seminfo; if (semctl (semid, 0, IPC_STAT, (union semun) { .buf = &seminfo }) == -1)