Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1236712rdb; Fri, 1 Dec 2023 10:17:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEMJ6g4ztIiwY8HGXb3ypvrOileO711CAic0/Pa7MwfNnMfHEvGsfA3MvHjdVgLIdvja5w6 X-Received: by 2002:a17:90b:1e47:b0:285:b78a:dbce with SMTP id pi7-20020a17090b1e4700b00285b78adbcemr21520761pjb.37.1701454639379; Fri, 01 Dec 2023 10:17:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701454639; cv=none; d=google.com; s=arc-20160816; b=x8DuIg/pTvru2yhpLDres+A1RuI4aZb8SI3GSzDnZQpKotxHjNqBIYr20FawI+whBC L3TwfODN/dRF+40CLA3t27sfu3BBZjP9yIUbC2OWrakbW646eCyR9ujMoel6uPxUuBHK a2VsBStItirC7J0+DyxBtfIKiWlC8MGqKy86ehfECd3Pd5yubF8KAfI8Wqyc2qYFiGdX i5U+bt4JbYifdiCnenDtvgViE0YDaEMxc9/pRDw2xN3ntRqJGLS+qAfQ8M1NR4UB3TiI ihOq/rWP+nYetYxTXa2kccf0t2nCXTI3GjpR94jI/IznmjumGsp/9R6DD0J+d1E/WZrk eIXw== ARC-Message-Signature: i=1; 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; bh=80BOfbzU3jRYQRqKUun9qybGXNm0Ak9QPhMAb8Oq5lY=; fh=B55XJ2IYMdGfi19ZtppEbQUQtkpfkhtt8Yi86chwXDI=; b=UWiztJyagiVZ2qOjwy3FTJISzOkEVEDnMRh5MTM9EcKRFDbGHnr+fsn3lnik3F4RnC WdOOZBztTQDEoUWG0bj8zIqRYdzNfTDO/xsJGj1tBUJkyN5HuaFv2FzGAN+T09COeTVs KqHu1AGkojXpgwmecd0nnNqCEpPX1IECFGb5/smrDLJ+qJIU5156g7UFVAV/K0iSOqSP iOqBq559GiuXXvdIwdpJbrFQNIfqxMCGKqsiec1Gi4Q6zqlM2Lhvfjtai6f/qvhiRMQI +dmDjHK+wxzL+ZpYanbAL4eLeAMC3TnEv2aHdS7rPE5celrHelpHg2wKGMUSruufq5iA ghNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ME+7xuSh; spf=pass (google.com: domain of linux-wireless+bounces-304-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-304-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id x13-20020a17090abc8d00b00280feff8f0fsi5761556pjr.127.2023.12.01.10.17.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:17:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless+bounces-304-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ME+7xuSh; spf=pass (google.com: domain of linux-wireless+bounces-304-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-304-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B7DEDB20D53 for ; Fri, 1 Dec 2023 18:17:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 52CEF4F1EA; Fri, 1 Dec 2023 18:17:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ME+7xuSh" X-Original-To: linux-wireless@vger.kernel.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B9EF10D for ; Fri, 1 Dec 2023 10:17:04 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1d065be370aso3981525ad.3 for ; Fri, 01 Dec 2023 10:17:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701454624; x=1702059424; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=80BOfbzU3jRYQRqKUun9qybGXNm0Ak9QPhMAb8Oq5lY=; b=ME+7xuShkkCd0062a9EL0L48UcVgl2XNVW5BRL6HrY/mg3DiShxSdpn7IJmQxj/w1W y6Kih+9DYWaLfoqYp+5pe938En1UXK6dzqyoieMKZZnN5wPV/Uq1I9TVzm7jMnnSzyry 5cZjnWvfXpQLdgXob0ttJuENteq2/BltyHmws= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701454624; x=1702059424; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=80BOfbzU3jRYQRqKUun9qybGXNm0Ak9QPhMAb8Oq5lY=; b=uTsyHrYLT3ExY6GPdVP/2OVCe7yCtwYs1YGqNeKMx/8WCkJKc/qEgEkGxpkNS+LOQt SmxCkQlWqYhhKP8KjUQ2HAsCivAcQkboYHLhMTVGFZA0Yp9+jpJvQE4fmJ78uW0jHpc8 WS84++UJIaROmW5UjEAkXNwlOmE0DDAw1jnyU2qgvphiOj65QsMgkonVmqrOvia7HTKO xSqL7+ZlT/lXb0o+Vb3FsHMrUecsU0WDhOs+Z76EyIUFuGSDBdfpj1i3VcrxDAcspgRD hrrEOFie8XgBAVxIc39rylyX/wWc2k+7UVU7CPAu6AL0JNRVAshrIKIeknUmrsBfiVb9 mdeA== X-Gm-Message-State: AOJu0YzM4guJtav8m1KGS3MBY3FFko+pbEgFzs/zzohDQKRIXUz+nHsj w+49B2oorp33hj7dh9fuTYIYKi2J/c6NTcGXjV8= X-Received: by 2002:a17:902:db06:b0:1cf:c376:6d7f with SMTP id m6-20020a170902db0600b001cfc3766d7fmr26246754plx.42.1701454623948; Fri, 01 Dec 2023 10:17:03 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001cfb99d8b82sm1570921plk.136.2023.12.01.10.17.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:17:03 -0800 (PST) Date: Fri, 1 Dec 2023 10:17:02 -0800 From: Kees Cook To: Jakub Kicinski Cc: kernel test robot , "David S. Miller" , Eric Dumazet , Paolo Abeni , Johannes Berg , Jeff Johnson , Michael Walle , Max Schulze , netdev@vger.kernel.org, linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] netlink: Return unsigned value for nla_len() Message-ID: <202312010953.BEDC06111@keescook> References: <20231130200058.work.520-kees@kernel.org> <20231130172520.5a56ae50@kernel.org> Precedence: bulk X-Mailing-List: linux-wireless@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: <20231130172520.5a56ae50@kernel.org> On Thu, Nov 30, 2023 at 05:25:20PM -0800, Jakub Kicinski wrote: > On Thu, 30 Nov 2023 12:01:01 -0800 Kees Cook wrote: > > This has the additional benefit of being defensive in the face of nlattr > > corruption or logic errors (i.e. nla_len being set smaller than > > NLA_HDRLEN). > > As Johannes predicted I'd rather not :( > > The callers should put the nlattr thru nla_ok() during validation > (nla_validate()), or walking (nla_for_each_* call nla_ok()). > > > -static inline int nla_len(const struct nlattr *nla) > > +static inline u16 nla_len(const struct nlattr *nla) > > { > > - return nla->nla_len - NLA_HDRLEN; > > + return nla->nla_len > NLA_HDRLEN ? nla->nla_len - NLA_HDRLEN : 0; > > } > > Note the the NLA_HDRLEN is the length of struct nlattr. > I mean of the @nla object that gets passed in as argument here. > So accepting that nla->nla_len may be < NLA_HDRLEN means > that we are okay with dereferencing a truncated object... > > We can consider making the return unsinged without the condition maybe? Yes, if we did it without the check, it'd do "less" damage on wrap-around. (i.e. off by U16_MAX instead off by INT_MAX). But I'd like to understand: what's the harm in adding the clamp? The changes to the assembly are tiny: https://godbolt.org/z/Ecvbzn1a1 i.e. a likely dropped-from-the-pipeline xor and a "free" cmov (checking the bit from the subtraction). I don't think it could even get measured in real-world cycle counts. This is much like the refcount_t work: checking for the overflow condition has almost 0 overhead. (It looks like I should use __builtin_sub_overflow() to correctly hint GCC, but Clang gets it right without such hinting. Also I changed NLA_HDRLEN to u16 to get the best result, which suggests there might be larger savings throughout the code base just from that change...) -- Kees Cook