Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp212435rwe; Wed, 31 Aug 2022 20:09:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR5nSsXCo5HcS6hawY97naiydClqpP1cP7KsV+amjsrYTWrB+IHIMNWBt1xyN9pH8C5kENQ8 X-Received: by 2002:a17:902:f08c:b0:173:3c34:a85b with SMTP id p12-20020a170902f08c00b001733c34a85bmr28382164pla.98.1662001740258; Wed, 31 Aug 2022 20:09:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662001740; cv=none; d=google.com; s=arc-20160816; b=NSwehSc7ZZV/S8nX9m5NExFuei8Vgf+EXKMqasXyIadeIgs2EGlaj9yToXKn0oeEkf fdCUnU0SMsiqhNZIWvgeqWDDnz1t2GSbygOkQPh94LYkOsKebHfBhe8PguwD3CSHn3Vs MPQpss3ILgQGz3lYaAvMZs2i0wjXTZllXuXpOoZnL2lHBl4cmTzuY8pd/l5Bfsef4HBI vsgb44dfQMdFbXjGqjZL5DEsDJJTY4scKlENKt18PVIFhaluqT4wMTkITyUkg25H31Jv G6ZeTe87D3EAhSqPjS4G26wDKhwXBjAR9N3ZK08ZE4TLDDcBXpUNjIIpG+eXyT8gCNeR GnQA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HBoZfnQNI+HfqlpbIESjGhVcV3oI45aOaNXfV3ZCT+M=; b=zQTc47qHVJzARAIkJbo8E5zdqh9YmwBSeGyICz3HjZEqhptjrBbA1hY0ph3dJS1a0K B8h1Y1/Ei+r/AOhE2N7LMXbl4ewj48dhfehTe1FwxzhUMskcl9HxnxzHX+xaG2ZryDyF TiNUcY4yCS1GPZLj7QiXYQhUCxsd8rBLu4UEfkP6QIfdE1p7REBKdIGBZL+cdOFDigGf riOWer8hR6bzbhHh/LE/0By+/DTcdHT5LgVHhEiuhLI+YXSp2zYZpsHT58qYEzjXMkDK Gg+7VghljnRbBIPu9RclK5tLEsy6mW5C7LF9rKlYnUafwm3S/zW0GJJU5+kk17F1Ga/O E5Lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eTuTwVYJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s13-20020a17090302cd00b00175284c8e9dsi5695675plk.199.2022.08.31.20.08.46; Wed, 31 Aug 2022 20:09:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eTuTwVYJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232265AbiIADGa (ORCPT + 99 others); Wed, 31 Aug 2022 23:06:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbiIADGY (ORCPT ); Wed, 31 Aug 2022 23:06:24 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30457E68F0 for ; Wed, 31 Aug 2022 20:06:16 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id d12so15864038plr.6 for ; Wed, 31 Aug 2022 20:06:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=HBoZfnQNI+HfqlpbIESjGhVcV3oI45aOaNXfV3ZCT+M=; b=eTuTwVYJ0tOmU7pb1JotZVoA2KlqihO9iBSjlDBRFwaMAH5iJ1lsOrdpz2Pe913xHf 8NqsUwncBSOtPvPbdv1/ONLmyfZ1lNFYDZ81/b/dFd6dBsv/uifMvEE+GJzCeiWHwfX6 x7FyuJIu4a/fYrGZJECePZTFpQ3sIYahfwxyQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=HBoZfnQNI+HfqlpbIESjGhVcV3oI45aOaNXfV3ZCT+M=; b=VKK+I74pCmV8OURu7pUt6zD6t2ctn6uBk4n8i/ZciX01kSJzXq8FZYFpQBI9o/i1+g isVEJIbOqrb3aWhfLPfGKh852gLSauyzwqXv6P2SpksY9uK5OTbN2KUPgfSc9sZuYzI2 hEGSwQgKcmu+L4EhzTJvXn9SFxo1Y2EjNwiXyyYIn3rMqvcUxiI0IA3p847Z8MrfV2Ji ie2kvvoikyAWdKGMk4yNKk3hQgC6EAYXqCLuDuEfHwusuJy/OHpQAhOew0h5aC7Xhgkn /VhoCOrkP0B/XS9FCDioRb6bD+Y3E4QGLonhbVv28wqbjq/pb/WpL6dJyqp3JXNCKS0n 0wvA== X-Gm-Message-State: ACgBeo1m7p9pEm3eIk1x7SCBi9GUEyAstA/Duqb2TxnqPLdMH26tA0sp qM3rQVzRnp766EytYZ+ZkdNyQg== X-Received: by 2002:a17:903:2d0:b0:172:b63b:3a1e with SMTP id s16-20020a17090302d000b00172b63b3a1emr28934419plk.76.1662001575695; Wed, 31 Aug 2022 20:06:15 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u17-20020a627911000000b00536b975b013sm11958015pfc.24.2022.08.31.20.06.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 20:06:14 -0700 (PDT) From: Kees Cook To: Jakub Kicinski Cc: Kees Cook , "David S. Miller" , Eric Dumazet , Paolo Abeni , Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , syzbot , Yajun Deng , netdev@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, Oliver Hartkopp , Harshit Mogalapalli , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] netlink: Bounds-check nlmsg_len() Date: Wed, 31 Aug 2022 20:06:09 -0700 Message-Id: <20220901030610.1121299-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220901030610.1121299-1-keescook@chromium.org> References: <20220901030610.1121299-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1489; h=from:subject; bh=kTIzPiI3XGTCF2tMjW+3PGFXwtUGmpz93ALVlrdAflg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjECGinowdI8+RbQIJuuvIuxm1f6BHZsT/hX1WUouh UA4VBdSJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYxAhogAKCRCJcvTf3G3AJlauEA Cjt6bHTa4IjL851iMSju08HCU9JQ6MwajPOCSHt4nnmCGRsSHXlfDDjuj2d58oqlprjPDnQfZud9FS DudDKadlPVZaEtXzu48IydekR5zXcFEBQxZ5bXgNrxSifHhPUy+5OAaJ3fNWmlhY2rh5ANa+rup4Rq VAq5mVSdWRlDQybKX6CmAVsa/vIbuwPHbRaYYX3/3g1/fDO5dwKs67QQ8QCq4OU8RVGLrJgWHJGmu0 mWZhKMqxJq2vcnGtKuo3BVdkgnAxBq3qgOJpMB8Ww45MyWkueEe2xsZ14gDzlMf2cnPwnqsjL5fbPz DnEOtSBNhoBrhTqp4CTuw8P/O/+UVvfcf5DnVhbD8TbPKU/yxmwWqbNQKfv7ullrEcskEGcCGmvhEJ lot0bIf3p/Ac8IRtXqoj+6lX9oOCAiJcx1Qhn7Cdel2YiRdliYS1GkJMiM3TFbXqXuNaX5kyAte9oJ /E2bNIkA3HSZjRGVZVjs5JehwOj6RKLaECUgTLzi6Jl3jJ/RKe3Gp3c8ysX3uCoj8o7OCNiDsFP8Is L00xAMueWLjxGfSChP0JJGc/SSOu17Vl8nGpk08bP/NHhQ0WZpntJbrnLPqinCibDd3mJoAjWPAS0x 6qeYdXnqrYDsDocjZeW1zdzaGdldu7u7vWN9p5o6DM2I3BMsjWCbilArMPAw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 The nlmsg_len() helper returned "int" from a u32 calculation that could possible go negative. WARN() if this calculation ever goes negative (instead returning 0), or if the result would be larger than INT_MAX (instead returning INT_MAX). Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Pablo Neira Ayuso Cc: Jozsef Kadlecsik Cc: Florian Westphal Cc: syzbot Cc: Yajun Deng Cc: netdev@vger.kernel.org Cc: netfilter-devel@vger.kernel.org Cc: coreteam@netfilter.org Signed-off-by: Kees Cook --- include/net/netlink.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/net/netlink.h b/include/net/netlink.h index 7a2a9d3144ba..f8cb0543635e 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -576,7 +576,15 @@ static inline void *nlmsg_data(const struct nlmsghdr *nlh) */ static inline int nlmsg_len(const struct nlmsghdr *nlh) { - return nlh->nlmsg_len - NLMSG_HDRLEN; + u32 nlmsg_contents_len; + + if (WARN_ON_ONCE(check_sub_overflow(nlh->nlmsg_len, + (u32)NLMSG_HDRLEN, + &nlmsg_contents_len))) + return 0; + if (WARN_ON_ONCE(nlmsg_contents_len > INT_MAX)) + return INT_MAX; + return nlmsg_contents_len; } /** -- 2.34.1