Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1027116iob; Fri, 13 May 2022 20:10:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQCld02WDbLq7VhUPCGFAlgSDP2W/7E5q5HwNOUd6yxfWK98vD3JIFjWSZHFjy20vloTuV X-Received: by 2002:a7b:ce82:0:b0:394:2514:6f07 with SMTP id q2-20020a7bce82000000b0039425146f07mr7076947wmj.56.1652497817366; Fri, 13 May 2022 20:10:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652497817; cv=none; d=google.com; s=arc-20160816; b=CWjl8D82j7gQxlrv8WIPbVUv0QGoU+BZ2fY1pPDMalOMCOLC323Nd9p7prnVTCtHY/ o6clUXfwuzt77k3YGV8Ibh2LwzYB1NyRQGs7XepzMuse/3ieHMJXad+QAQA5/z65z3pB 1kRbvJUFJRSs16/lmVcY6kdqMui8TdEwAOziSx5kXzYnuG+zBl/Aod7W5NrTXY0X4sqI q83lMbA11mWkxc44ELqPg1cCDK2Tsr+l95HH1gaD85l4p0dy+OdIk1s20hTBY/wCzW97 FrwBMvStD+4MpTz6lYKJ++UJFlmNZokxqvZUlHu6OBdyfVIy26SivLgwyB8hJpFu9j1L VEBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=OekgtfVu30IY1xKeHSv6R2Im8PYHy95CtVdddoMCPic=; b=FyNVc5reQUraDmkl/UGqE7nTmECEGgTIAG8uO87HsU7s9LsPWb1JLBHE0jaDewkx+5 vtifZ5B/px+ZmCXGKx3x/vqjy1e26WEUA693VSCtyM/5lnR/yCoVhLZBSDiatctaIxfU +goq/D6udCO7f+/cdMlh+pjpYACiozmfqcBnvqTSAhmXhTlCIjNmLwe4KFBC0KnUXUHw hs7hP+XvSsE1MaElIE26Q4oNf+3icQiMi6irMgxgh3wLltUWcwBQqzqbgSzdjCcuJ5rs LHCwid23Kc1/0YjCG3rpwEC+6y3iiBiiXQnOCBNYOzKsTHn7MZQ3/vZaPGQQq8UDao/M mtzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TzoB19gd; 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=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id a10-20020a05600c224a00b0038cadd9e7a3si3724928wmm.14.2022.05.13.20.10.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 20:10:17 -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=@intel.com header.s=Intel header.b=TzoB19gd; 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=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D61AD3C85ED; Fri, 13 May 2022 16:50:58 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357589AbiELSW1 (ORCPT + 99 others); Thu, 12 May 2022 14:22:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357594AbiELSWU (ORCPT ); Thu, 12 May 2022 14:22:20 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 608F860B86; Thu, 12 May 2022 11:22:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1652379732; x=1683915732; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=gKX1PGO+dptTOabwhDDmbl7BBre5vJXGH8K4d83YX8E=; b=TzoB19gd4P6Hh0YJNn4nRgFSA80K6pIBw1/WOKUsa/SPgfJ4xLECcLSh 7DwU4Q+z3+KSOAGAfj5pdGp71tlh9BRx2g6N93FuBg4MJAXrJGtyi8PIA Qp9TSlZXfLAa3KzNhpk5ej45qDkrD5CEEwJWGJUkPt1cfNcy1MvlULszS 5b435MrU+3J+NYd7s8tzgtqRYv8u0rbM9J+fDmTv/AR5phbMYhmssxXfG vXbkdPcbZwE3eyiGupaWTf+2RHYpykmQI0t91dc/TxO5BEzd8xljoW864 3mNoGWuSFaNxGyQG9bJkb/DkUflrZbxFP/sNGSCSbWoRN7gi6dw3Xp3l+ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10345"; a="267690652" X-IronPort-AV: E=Sophos;i="5.91,220,1647327600"; d="scan'208";a="267690652" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 May 2022 11:22:11 -0700 X-IronPort-AV: E=Sophos;i="5.91,220,1647327600"; d="scan'208";a="624521577" Received: from josdenl-mobl1.amr.corp.intel.com (HELO ldmartin-desk2) ([10.251.19.41]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 May 2022 11:22:11 -0700 Date: Thu, 12 May 2022 11:22:09 -0700 From: Lucas De Marchi To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, Jann Horn , Kees Cook , Luis Chamberlain , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH 1/2] sysctl: read() must consume poll events, not poll() Message-ID: <20220512182209.7uiy3pt4chctqhg4@ldmartin-desk2> X-Patchwork-Hint: comment References: <20220502140602.130373-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20220502140602.130373-1-Jason@zx2c4.com> X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, 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=unavailable 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 On Mon, May 02, 2022 at 04:06:01PM +0200, Jason A. Donenfeld wrote: >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 It seems to be my bug. This is indeed better. Also, I don't think it's unsafe to fix it like this neither. If my memory serves (it's what, 10+ years?), this was only tested and used with poll(), which will continue to work. There were plans to use it in one of systemd's tools, in which case we'd probably notice the misbehavior with epoll().... humn, checking now systemd's codebase: static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { ... log_info("System hostname changed to '%s'.", full_hostname); ... } static int manager_watch_hostname(Manager *m) { int r; assert(m); m->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); if (m->hostname_fd < 0) { log_warning_errno(errno, "Failed to watch hostname: %m"); return 0; } r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m); if (r < 0) { if (r == -EPERM) /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */ m->hostname_fd = safe_close(m->hostname_fd); else return log_error_errno(r, "Failed to add hostname event source: %m"); } .... } and sd_event library uses epoll. So, it's apparently not working and it doesn't seem to be their intention to rely on the misbehavior. This makes me think it even deserves a Cc to stable. Reviewed-by: Lucas De Marchi Lucas De Marchi >--- > 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 >