Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp183636iob; Mon, 2 May 2022 16:32:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJlU33Tr9bPdIamLHoGdKXxJ3XS0O52I8FIZc/LWXYVB7Enf+C89xQ/hnmL80N0UVS+PxJ X-Received: by 2002:a17:90a:7acb:b0:1d9:85a5:e1e3 with SMTP id b11-20020a17090a7acb00b001d985a5e1e3mr1690147pjl.172.1651534345351; Mon, 02 May 2022 16:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651534345; cv=none; d=google.com; s=arc-20160816; b=BWCWzs9yGR5k4FV6vJSCt7tsLjFtZpLjls26zafuRt9chG/5v25KceqT9B18ItxNPU 3tb/1AGsqANlY8pSkFrt0hDo9QqgOmeIKkN2p+ouxW4fcTXZ1/fq8RG5V4qptGvxQdmX muROjjAenLTVrIJx75kmQr9lc/bvR5eYnwqf9iJadDlt5KJqJ7MkNsoMdxiyFsmJWLsE wXOc4Hv1v+37G3+4SbbHWSgXioFL7LGz//8vwJLQ55OUsp77M4Dk/dOKPdjGJPATytpt Knw2veMsnCILOMo8aAjyQCvECddWbUcW6nXAEgBacKosTbI0jTNICbuqRuKxi4bM2M0o hc0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=+7+HGir1Cy6uU4bq/QSK6Sa0K4vOQe5UFQEDdB+7RMI=; b=l2xRjaVJu9hN1XVh4dXE3xEPDMfI2+PcNTLr0jxDZ5fxB2XY6vPa7Df0gls+6eW1T+ F9+cILYx+6J34MwqrB+bsSo30pQ/hpPXXdhm9bEBgHW8kHEX2Xu8QGp4/8Jo49jqeuY3 xIZgMjFw73gQUdgg0SPKIUJHRBuyD3vxP11DR9bFtnDElBufXD4p4Z2w3IREhoo6WT/z EwbOC1KN0roCXgSYP0ItzKMHj3WDCQRzdGYUUjA1jnqsiKiAWg7QDhtVaEQ94O+pZ7CB 6TgwtOvcvkpP2CbSczxBpFU9GD5d8Koz51DGkSFwQYkxSLevj2iVpLt0B3WBMVxyrxP8 Htsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=eWJc49DY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id x2-20020a63cc02000000b003c257a836b2si2093700pgf.192.2022.05.02.16.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 16:32:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=eWJc49DY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0144731370; Mon, 2 May 2022 16:32:18 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236808AbiEBOJz (ORCPT + 99 others); Mon, 2 May 2022 10:09:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236744AbiEBOJy (ORCPT ); Mon, 2 May 2022 10:09:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EECAEE03F; Mon, 2 May 2022 07:06:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6120AB8115F; Mon, 2 May 2022 14:06:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7230AC385AC; Mon, 2 May 2022 14:06:21 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="eWJc49DY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1651500379; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=+7+HGir1Cy6uU4bq/QSK6Sa0K4vOQe5UFQEDdB+7RMI=; b=eWJc49DY3MlUFo3eogAJqMeRkRrRIoCzUvKiQAh5btUyGLkMzSvCJcS5QPFb2l8HKapJrg nQE7N4FjRz4A5porfk0mUwLD5XcKSjb9z3rFmNGVwvS3vfTMF8Lfpu6EGhjL6xXtTqxOuQ UuocIv+v31Sgk/OWwIhfVawGtbu61Z8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 160ad175 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Mon, 2 May 2022 14:06:18 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Cc: "Jason A. Donenfeld" , Jann Horn , Kees Cook , Luis Chamberlain , linux-fsdevel@vger.kernel.org Subject: [PATCH 1/2] sysctl: read() must consume poll events, not poll() Date: Mon, 2 May 2022 16:06:01 +0200 Message-Id: <20220502140602.130373-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Events that poll() responds to are supposed to be consumed when the file is read(), not by the poll() itself. By putting it on the poll() itself, it makes it impossible to poll() on a epoll file descriptor, since the event gets consumed too early. Jann wrote a PoC, available in the link below. Reported-by: Jann Horn Cc: Kees Cook Cc: Luis Chamberlain Cc: linux-fsdevel@vger.kernel.org Link: https://lore.kernel.org/lkml/CAG48ez1F0P7Wnp=PGhiUej=u=8CSF6gpD9J=Oxxg0buFRqV1tA@mail.gmail.com/ Signed-off-by: Jason A. Donenfeld --- fs/proc/proc_sysctl.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 7d9cfc730bd4..1aa145794207 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -622,6 +622,14 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter) { + struct inode *inode = file_inode(iocb->ki_filp); + struct ctl_table_header *head = grab_header(inode); + struct ctl_table *table = PROC_I(inode)->sysctl_entry; + + if (!IS_ERR(head) && table->poll) + iocb->ki_filp->private_data = proc_sys_poll_event(table->poll); + sysctl_head_finish(head); + return proc_sys_call_handler(iocb, iter, 0); } @@ -668,10 +676,8 @@ static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) event = (unsigned long)filp->private_data; poll_wait(filp, &table->poll->wait, wait); - if (event != atomic_read(&table->poll->event)) { - filp->private_data = proc_sys_poll_event(table->poll); + if (event != atomic_read(&table->poll->event)) ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; - } out: sysctl_head_finish(head); -- 2.35.1