Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp638200rdb; Tue, 23 Jan 2024 10:00:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFzqlWcUMKTTaQ4NdiJU+m77ADeEt8UWH8q1ZQrrDV0fwiNkDFeG9fj+88Y1Lk/roKI9ZdV X-Received: by 2002:aa7:d8d2:0:b0:55c:6ca7:7d52 with SMTP id k18-20020aa7d8d2000000b0055c6ca77d52mr1095994eds.68.1706032833145; Tue, 23 Jan 2024 10:00:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706032833; cv=pass; d=google.com; s=arc-20160816; b=zpOljb2BPpqnMn3YHOPhc+PCAxZE42PPXg13u4LiZs5jENFrebnCA9uPM00hU3E9Ib y4RbNRp/lKTiaWTONh/S+6lLxb0I78q32+IV0f5b4A2iGvNqIWg72KPGP7GZDrKlDIDE mYQ+lEudOUPJSHLvYHagttdJM7heVQookvqBC0LfvgJq57v0oKa5s7Gzbx4fsUiNxyYy bAdgOW60v1mxDAxwti+NYCXwNg4Nq7zmlYmi6bbwsxw4bJvqlEXPP5H8dpp4Pz+rtmO6 NFM/pODKKlR9yMo4OkkQOi5tRB2zsaT8oiS6LG9mQpqo1IM91H9uInPJFzvqOaPSR+ia rq4w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature; bh=INBI/Isa3O19dYdw2aRIskylv6DV2Svn3LWWNjvnDaE=; fh=C44aXBuIpWgxfRPO9w7kSKSqSTPTsxsF2irGPf324zo=; b=bkMCnnACM+9+T/bp0Hv+yeC8+hwYOQrTonYX8gNaJo2vbcFWDcirw095uqYmqhLJTW C+keuoQQGAG8Axu3sCgeOQ2VyAO6q+rn+UO38NaGtwdWvFxpXMaD0i3QtAC72MOU1N92 AP4gGfNM2x74NxS5Yi0QzI6JgmWdYU2yMQQTNUHLMJeFDrNmH4nhc9YBVjRzFntzfXiS b9QJY4JA3a8kj6PLqBHQZxNHUWodAut+wBKSLxVMsJEJAYJO4mCgER41i8QwrwiQ8uFr LgGHfCckeq1UmFQa3vSzjk1GrrTVv4UHW/IQGy+mjNaum3TVO92Pi0EKnfBQsqZj1nNv w0Bg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b="kiV/UvtX"; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=N7RwBA6F; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b="7+M8i/DF"; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-35832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35832-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h13-20020a0564020e0d00b0055c8d9e1eb4si697769edh.227.2024.01.23.10.00.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 10:00:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b="kiV/UvtX"; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=N7RwBA6F; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b="7+M8i/DF"; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-35832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35832-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BFAE31F285B2 for ; Tue, 23 Jan 2024 18:00:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0E60C811F2; Tue, 23 Jan 2024 18:00:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="kiV/UvtX"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="8T+6+jW/"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="N7RwBA6F"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="7+M8i/DF" Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65FC38005D; Tue, 23 Jan 2024 18:00:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706032819; cv=none; b=ndCEyi2fEw101xKgWIb8jXU6md2fzynAOYfXMrP4GRUecYmX2mBFAQW5MD//T6hmCgGcJl+5+sCeF2U+4aa4Dj0tCATLvkTv6Xoi3SXDzStd7Odze1hpAcYpW33mZC5bjwhqAbLqHoWk/VZ/0T+1ZTNSqKN+DIBESPc4gJ40NQk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706032819; c=relaxed/simple; bh=x+4DKQ0VomY5Vn/A2UNegg9hB/vO0L2w7CrEM1I0bng=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fnjPrhT2WvC1x3GKUCaRgvQLMxPYpfm3reUCQcadUXJHxdkqZDCqBaBR9WUjjumZeUNi5XJW2Fui18bfWZYZ0CV+CwW9gCE95UKs5VGr5k4vRYMXhLrg24geaCIt/XGfj8mS/yAYixA4moG3NrJCX8Kx/uxvBsUDZFLV4n2LdxU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=kiV/UvtX; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=8T+6+jW/; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=N7RwBA6F; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=7+M8i/DF; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.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 smtp-out2.suse.de (Postfix) with ESMTPS id C6EDB1F79B; Tue, 23 Jan 2024 18:00:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706032809; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=INBI/Isa3O19dYdw2aRIskylv6DV2Svn3LWWNjvnDaE=; b=kiV/UvtXntEAK8kIull+dRj6dexryLwH4imrKZ+MyXmAyrOhkr15RuzMGIaJXXintJjb7e t9KDU1IHCFB5jye/TKO/8kQaPHhwXWfxfywZc3NhfSWn+VRILt5XJlRbWInjxSXoLXkB43 /rgsB7hoQzs13LHiHqqmiM/ALAOeoCo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706032809; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=INBI/Isa3O19dYdw2aRIskylv6DV2Svn3LWWNjvnDaE=; b=8T+6+jW/0prvyKoDs1LZw2bQpHKrm7TScVec8e7i4wyErKLJ2+U4Tv2LtVIUC8jXdWG8Nx rbKaqYqt20SaLBCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706032808; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=INBI/Isa3O19dYdw2aRIskylv6DV2Svn3LWWNjvnDaE=; b=N7RwBA6FBOSZIY00nSEYwyStKOhMVag1aZC5AFlB8kbvvE8XfiBW3uD4f4R7SnVSomsJY/ 56xfcM/wdX9uvklhm5q0n23/0ad6leOz7cnJjFODvnuFsgKIICL9jHBE2+2nMJ1WNxNfOq XcM+BNrYfQbI7ywxPshpnzX8VsC3TUY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706032808; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=INBI/Isa3O19dYdw2aRIskylv6DV2Svn3LWWNjvnDaE=; b=7+M8i/DF36t2tADhHtScro2yUi1M09IQuMjQ8Igq2MsJYPa2feIo97ElujbXiNZGgkfFCz TdZQc9TArcnaAlDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B8601136A4; Tue, 23 Jan 2024 18:00:08 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id lZ/zLKj+r2XcKAAAD6G6ig (envelope-from ); Tue, 23 Jan 2024 18:00:08 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 53DFFA0803; Tue, 23 Jan 2024 19:00:08 +0100 (CET) Date: Tue, 23 Jan 2024 19:00:08 +0100 From: Jan Kara To: Kees Cook Cc: linux-hardening@vger.kernel.org, Alexander Viro , Christian Brauner , Jan Kara , linux-fsdevel@vger.kernel.org, "Gustavo A. R. Silva" , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org Subject: Re: [PATCH 09/82] select: Avoid wrap-around instrumentation in do_sys_poll() Message-ID: <20240123180008.sg77hnu5r7nqrgjy@quack3> References: <20240122235208.work.748-kees@kernel.org> <20240123002814.1396804-9-keescook@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240123002814.1396804-9-keescook@chromium.org> Authentication-Results: smtp-out2.suse.de; none X-Spamd-Result: default: False [-2.60 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; RCPT_COUNT_SEVEN(0.00)[10]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:email,linux.org.uk:email,chromium.org:email,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.60 On Mon 22-01-24 16:26:44, Kees Cook wrote: > The mix of int, unsigned int, and unsigned long used by struct > poll_list::len, todo, len, and j meant that the signed overflow > sanitizer got worried it needed to instrument several places where > arithmetic happens between these variables. Since all of the variables > are always positive and bounded by unsigned int, use a single type in > all places. Additionally expand the zero-test into an explicit range > check before updating "todo". > > This keeps sanitizer instrumentation[1] out of a UACCESS path: > > vmlinux.o: warning: objtool: do_sys_poll+0x285: call to __ubsan_handle_sub_overflow() with UACCESS enabled > > Cc: Alexander Viro > Cc: Christian Brauner > Cc: Jan Kara > Cc: linux-fsdevel@vger.kernel.org > Signed-off-by: Kees Cook Looks good. Feel free to add: Reviewed-by: Jan Kara Honza > --- > fs/select.c | 13 +++++++------ > 1 file changed, 7 insertions(+), 6 deletions(-) > > diff --git a/fs/select.c b/fs/select.c > index 0ee55af1a55c..11a3b1312abe 100644 > --- a/fs/select.c > +++ b/fs/select.c > @@ -839,7 +839,7 @@ SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg) > > struct poll_list { > struct poll_list *next; > - int len; > + unsigned int len; > struct pollfd entries[]; > }; > > @@ -975,14 +975,15 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, > struct timespec64 *end_time) > { > struct poll_wqueues table; > - int err = -EFAULT, fdcount, len; > + int err = -EFAULT, fdcount; > /* Allocate small arguments on the stack to save memory and be > faster - use long to make sure the buffer is aligned properly > on 64 bit archs to avoid unaligned access */ > long stack_pps[POLL_STACK_ALLOC/sizeof(long)]; > struct poll_list *const head = (struct poll_list *)stack_pps; > struct poll_list *walk = head; > - unsigned long todo = nfds; > + unsigned int todo = nfds; > + unsigned int len; > > if (nfds > rlimit(RLIMIT_NOFILE)) > return -EINVAL; > @@ -998,9 +999,9 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, > sizeof(struct pollfd) * walk->len)) > goto out_fds; > > - todo -= walk->len; > - if (!todo) > + if (walk->len >= todo) > break; > + todo -= walk->len; > > len = min(todo, POLLFD_PER_PAGE); > walk = walk->next = kmalloc(struct_size(walk, entries, len), > @@ -1020,7 +1021,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, > > for (walk = head; walk; walk = walk->next) { > struct pollfd *fds = walk->entries; > - int j; > + unsigned int j; > > for (j = walk->len; j; fds++, ufds++, j--) > unsafe_put_user(fds->revents, &ufds->revents, Efault); > -- > 2.34.1 > -- Jan Kara SUSE Labs, CR