From patchwork Thu Jul 15 20:46:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12381103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC7FBC636CA for ; Thu, 15 Jul 2021 20:48:31 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7D824610C7 for ; Thu, 15 Jul 2021 20:48:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D824610C7 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Mime-Version: Message-Id:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=+pKJOwlgMDCjA5YeUev1foIvgqQFbhS1BrQ1AMY9/JU=; b=1Sf rT5/umYHXEUHETg46AA8ZYjTvkFCmBGfzvkIEaOrbIo3L/N0lzGotofEB0VYpDWxt9jjjdkurd5MY YTls6DgLG2p79eHgPbgXPSmZo8WpRBpQGLxaTPHUHmF+o57u66ilGMFAePUw5cklRkABIx9rRlJ/I FzDBN5lhiimIeRXsGPS0PIMkyWj+FMNOQVTnGLo7jJ5VQLvB4d2/ETZqAiU6mRs/9YF345+WixPZK f2L2I20kQ7HhGRYUDbAa6jeJhLq3HwlI92aD7KjlrVfLjQ4s+MXVU3hEeI17ZLgtclWcsYz8PL8NF kcQ+QYeOzU+BAX/AgyyaD8gaFy2D6dw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m48FY-002K9D-HA; Thu, 15 Jul 2021 20:46:28 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m48FU-002K80-0G for linux-arm-kernel@lists.infradead.org; Thu, 15 Jul 2021 20:46:25 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id x15-20020a25ce0f0000b029055bb0981111so9506970ybe.7 for ; Thu, 15 Jul 2021 13:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=py55da35odqXUKPz6mWY1d6sBAOGNrGbD2bahMQ95KA=; b=uqGtzYpj2e8Z7+9ZlSjER81JDMWTIgZmRpSoY38r/yzbX63hLoY5Ns2VG8zSRAyxPH pT4LcI38pq2Kl7GvVXCBZ83eaWZkMgFOO4JGpqsVuFgKLBIrHoFesbxv/iEnPPk6jn51 xtnVXYn0rK2hSQE41rsOZHVKiIcDIGAY9PDk4vB1eKADuDr8q/CXrAYqsXoN8zxSWVvv nrCUcXqOLRwuOTdvRy+xweAFlHuCPTw4Crf01lzsU1nSsNuPJcyLkAZ5FAm/bxK2sLjn jLGBWUGAPKJ/xmPBTSPUpbdTBBnYhM6HW3MBSUA3GCMb1bqYhesNs5ehiKuMD9hHpZ/i Zofw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=py55da35odqXUKPz6mWY1d6sBAOGNrGbD2bahMQ95KA=; b=Wh+lQ8e1MXPVm1n6gvfZALieOsv874XCU3Mb3mXqnZEJal2nMSMzIXcQMkB2p349KW 7g22zu+QrO9FJJ48MB0iFy9YW/SJbeLg9jRi1bNiaclW9hHgJU1fP/+apzGnXX85BGDZ 9TIm1DSwxW3Zq/xq5Z0ownBDeWAz3kxRvnTvFeT8MtdJ7MDBK1SlEPK+HS+kPPlgDCc/ y3GTLIxaUdBJe8Uwbo//pkBRlXfHGlCabJr9utxdHxkkYoJNu73ZWq0QaYUViQkRdYiy NGvYteb5MS3aUE/deppas/FOd1ezs0jn8f9Lf8ksYVT2pw0MeAr3nY1OQDTK60WmnQGN uCsg== X-Gm-Message-State: AOAM531Fun+IPHm9VQfCtX25EnX96IMZslqE3CpOJ+gsBA4f1OrFZv9l IOaUkYgNE2mrFxpXZQ1Y0otY3Gk= X-Google-Smtp-Source: ABdhPJxhM8MljKIEIqE1GyuEISKGfborLYfirArTvxCiqV41qv3cOIt4ZBh4F8EmX8+WHMIUosb9VwI= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:5c63:3fc6:856c:f8d]) (user=pcc job=sendgmr) by 2002:a5b:801:: with SMTP id x1mr7983259ybp.137.1626381977487; Thu, 15 Jul 2021 13:46:17 -0700 (PDT) Date: Thu, 15 Jul 2021 13:46:10 -0700 Message-Id: <20210715204610.575999-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH v4] sigaction.2: Document SA_EXPOSE_TAGBITS and the flag support detection protocol From: Peter Collingbourne To: Catalin Marinas , Evgenii Stepanov , Kostya Serebryany , Vincenzo Frascino , Dave Martin , Will Deacon , Oleg Nesterov , "Eric W. Biederman" , "James E.J. Bottomley" , Michael Kerrisk , Alejandro Colomar Cc: Peter Collingbourne , Linux ARM , Kevin Brodsky , Andrey Konovalov , linux-api@vger.kernel.org, Helge Deller , David Spickett , linux-man@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210715_134624_109924_7F344310 X-CRM114-Status: GOOD ( 18.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Signed-off-by: Peter Collingbourne --- v4: - s/.BR/.BR/ in one location - 4 space indentation - raise(SIGSEGV) instead of manual SIGSEGV - use C99 initialization v3: - s/5.x/5.11/g - s/.IR/.I/ in one location v2: - fix formatting - address feedback from Dave man2/sigaction.2 | 123 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/man2/sigaction.2 b/man2/sigaction.2 index 57ad6418c..4bf6f095e 100644 --- a/man2/sigaction.2 +++ b/man2/sigaction.2 @@ -261,6 +261,44 @@ This flag is meaningful only when establishing a signal handler. .\" .I sa_sigaction .\" field was added in Linux 2.1.86.) .\" +.TP +.B SA_UNSUPPORTED +Used to dynamically probe for flag bit support. +.IP +If an attempt to register a handler succeeds with this flag set in +.I act->sa_flags +alongside other flags that are potentially unsupported by the kernel, +and an immediately subsequent +.BR sigaction () +call specifying the same signal number n and with non-NULL +.I oldact +yields +.B SA_UNSUPPORTED +.I clear +in +.IR oldact->sa_flags , +then +.I oldact->sa_flags +may be used as a bitmask +describing which of the potentially unsupported flags are, +in fact, supported. +See the section "Dynamically probing for flag bit support" +below for more details. +.TP +.BR SA_EXPOSE_TAGBITS " (since Linux 5.11)" +Normally, when delivering a signal, +an architecture-specific set of tag bits are cleared from the +.I si_addr +field of +.IR siginfo_t . +If this flag is set, +an architecture-specific subset of the tag bits will be preserved in +.IR si_addr . +.IP +Programs that need to be compatible with Linux versions older than 5.11 +must use +.B SA_UNSUPPORTED +to probe for support. .SS The siginfo_t argument to a SA_SIGINFO handler When the .B SA_SIGINFO @@ -846,6 +884,91 @@ Triggered by a .BR seccomp (2) filter rule. .RE +.SS Dynamically probing for flag bit support +The +.BR sigaction () +call on Linux accepts unknown bits set in +.I act->sa_flags +without error. +The behavior of the kernel starting with Linux 5.11 is that a second +.BR sigaction () +will clear unknown bits from +.IR oldact->sa_flags . +However, historically, a second +.BR sigaction () +call would typically leave those bits set in +.IR oldact->sa_flags . +.PP +This means that support for new flags cannot be detected +simply by testing for a flag in +.IR sa_flags , +and a program must test that +.B SA_UNSUPPORTED +has been cleared before relying on the contents of +.IR sa_flags . +.PP +Since the behavior of the signal handler cannot be guaranteed +unless the check passes, +it is wise to either block the affected signal +while registering the handler and performing the check in this case, +or where this is not possible, +for example if the signal is synchronous, to issue the second +.BR sigaction () +in the signal handler itself. +.PP +In kernels that do not support a specific flag, +the kernel's behavior is as if the flag was not set, +even if the flag was set in +.IR act->sa_flags . +.PP +The flags +.BR SA_NOCLDSTOP , +.BR SA_NOCLDWAIT , +.BR SA_SIGINFO , +.BR SA_ONSTACK , +.BR SA_RESTART , +.BR SA_NODEFER , +.BR SA_RESETHAND , +and, if defined by the architecture, +.B SA_RESTORER +may not be reliably probed for using this mechanism, +because they were introduced before Linux 5.11. +However, in general, programs may assume that these flags are supported, +since they have all been supported since Linux 2.6, +which was released in the year 2003. +.PP +The following example program exits with status 0 if +.B SA_EXPOSE_TAGBITS +is determined to be supported, and 1 otherwise. +.PP +.EX +#include +#include +#include + +void handler(int signo, siginfo_t *info, void *context) { + struct sigaction oldact; + if (sigaction(SIGSEGV, 0, &oldact) == 0 && + !(oldact.sa_flags & SA_UNSUPPORTED) && + (oldact.sa_flags & SA_EXPOSE_TAGBITS)) { + _exit(0); + } else { + _exit(1); + } +} + +int main(void) { + struct sigaction act = {0}; + act.sa_flags = SA_SIGINFO | SA_UNSUPPORTED | SA_EXPOSE_TAGBITS; + act.sa_sigaction = handler; + if (sigaction(SIGSEGV, &act, 0) != 0) { + perror("sigaction"); + return 1; + } + + raise(SIGSEGV); +} +.EE .SH RETURN VALUE .BR sigaction () returns 0 on success; on error, \-1 is returned, and