Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp5020561ybv; Mon, 17 Feb 2020 10:32:36 -0800 (PST) X-Google-Smtp-Source: APXvYqyJatqy8HkyJTpFz3FjYmZjWg1/1K6Hpw3dfz8bg1ObMjiszFnAwNlfKOkTas+KY2DIBW4O X-Received: by 2002:a05:6830:1e64:: with SMTP id m4mr13556066otr.244.1581964356094; Mon, 17 Feb 2020 10:32:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581964356; cv=none; d=google.com; s=arc-20160816; b=g0M2n6+sMJyNQEl+FMV8GHPTwmqWX4x1mEhpfCXBslAuSD0owDMvnQ8uLHy9u9Hkf6 zzeGl2eL2WnYefaICWUdkb5ucZbcIsbG5WsC9m8T0yd8DYzhZF7mFEi9N8Qgm8qiG0iR 7xCJgn1t2jIw/dNs2CzZ/Lf1u+nxGgU0y5HpSx79byuZWTPQyGv7m3sndq7AttmZmDiX HfZaeYg+Y0w820njfg0Ru2P9XfKhps9qawsMXkSYTIh0eh/MzGKf1nuJL+zikR5pfuES vwvIA4GRMEuhgEDXTnmEyYV1p/wGbDkWXW66n+qll0P16/9QV/SDhKEXD/Pl9CDNHMCd DC5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=+y8jRH9et24PzZEzAuvtS6mrYS3E40060UY4DYXNlPU=; b=i02iEx2nL/LbHi1T1RoDV3guTEgHCe2UIH0Ym2CwOEHnDJvKe7z2GtZ1o3tPn3rCBA y2I0PusXnYox2T+BD9LIkfagbVraPm5emyqBu2krrW+AwcbZfwSQ5HLTWxzsNoEud+qx 7PtXxpgir5mFASelp4DVFm8gfv/PNgtMGuZH+mLO5er46AJeSXVy3auWXr19wtFMSQ89 BPHD5c259u4OYL3jKZQ2gyuaAQ3nUzxQoWfFrvscQAv5lV3VA0SVvwZKJFv/MThpxXFZ sNzZOaLlB6gEsQ/ddGMx/hnJABuXkjqaFampSRFWjerjqyhCcSt88lySiNNQa8Q6XelO mdwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XdybOT4f; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q6si556215oth.26.2020.02.17.10.32.23; Mon, 17 Feb 2020 10:32:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XdybOT4f; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728901AbgBQSZY (ORCPT + 99 others); Mon, 17 Feb 2020 13:25:24 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:33853 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726833AbgBQSZY (ORCPT ); Mon, 17 Feb 2020 13:25:24 -0500 Received: by mail-pg1-f196.google.com with SMTP id j4so9639054pgi.1 for ; Mon, 17 Feb 2020 10:25:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+y8jRH9et24PzZEzAuvtS6mrYS3E40060UY4DYXNlPU=; b=XdybOT4fn4GZR7RpSc1kSHox+vYiKKtchga8ALKVPxCgX40ZIME8RK3kjkFiNPSm38 uveaXH2a1VHQ2Fz8wm3Du+ivcowo1Lpxla2Ucef+7rkrfw9WQw1MbzSbkIZ0bS0qkUHw hQg2oHMweOiuO/de7+09ohgPaxuSgSs8sarCA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=+y8jRH9et24PzZEzAuvtS6mrYS3E40060UY4DYXNlPU=; b=SIRjpIycQ/En/tu1XKJIkJhqi9T811UFfNhVjFjfE0pplaWq3k7NdnSjyKrlpRuyzL Uc99237SmqYrKV3mMHlEqOaYZWNkEsw7YiVJLPKBGDEGk6TBTKt6VmfwLvLUpwaogUCL Y4qKfyGiQ4K4FaRJi77r5xZh/qkpFDHJ+t2TNTkxWqtucAvDMQZdMuksem6nyy5GMbwF PofqW3FgGhVYNz0tzYHvUBSxUKsY2UkyKSrX2DuJCByosdeeYyhRi+4gu1lWLvd3/xxN 8n9ZOh0VngPqrHGD3NfV0mZfssMD5bkIP9O0PsGbkdVezFPHc2dMWdAHEeE1bV6rxWmT 1Ufw== X-Gm-Message-State: APjAAAUszEsG3MWxMOTn9R22k8N6lLw+hFsT2yIvu1bzNk0axDi0pAeW gERbNXMWvtQdq0eNFtQ4v9WSmg== X-Received: by 2002:a17:90b:4004:: with SMTP id ie4mr372116pjb.49.1581963923632; Mon, 17 Feb 2020 10:25:23 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id f8sm1143089pfn.2.2020.02.17.10.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2020 10:25:22 -0800 (PST) Date: Mon, 17 Feb 2020 10:25:22 -0800 From: Kees Cook To: Daniel Borkmann Cc: Alexei Starovoitov , linux-kernel@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , Sami Tolvanen , bpf@vger.kernel.org Subject: Re: [PATCH] bpf: Avoid function casting when calculating immediate Message-ID: <202002171024.184D90B@keescook> References: <202001291335.31F425A198@keescook> <92bcfdab-79df-c3f4-bae8-00116b39e015@iogearbox.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <92bcfdab-79df-c3f4-bae8-00116b39e015@iogearbox.net> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Feb 17, 2020 at 04:51:46PM +0100, Daniel Borkmann wrote: > On 1/29/20 10:36 PM, Kees Cook wrote: > > In an effort to enable -Wcast-function-type in the top-level Makefile > > to support Control Flow Integrity builds, rework the BPF instruction > > immediate calculation macros to avoid mismatched function pointers. Since > > these calculations are only ever between function address (these are > > not function calls, just address calculations), they can be cast to u64 > > instead, where the result will be assigned to the s32 insn->imm. > > > > Signed-off-by: Kees Cook > > --- > > include/linux/filter.h | 6 +++--- > > kernel/bpf/hashtab.c | 6 +++--- > > kernel/bpf/verifier.c | 21 +++++++-------------- > > 3 files changed, 13 insertions(+), 20 deletions(-) > > > > diff --git a/include/linux/filter.h b/include/linux/filter.h > > index f349e2c0884c..b5beee7bf2ea 100644 > > --- a/include/linux/filter.h > > +++ b/include/linux/filter.h > > @@ -340,8 +340,8 @@ static inline bool insn_is_zext(const struct bpf_insn *insn) > > /* Function call */ > > -#define BPF_CAST_CALL(x) \ > > - ((u64 (*)(u64, u64, u64, u64, u64))(x)) > > +#define BPF_FUNC_IMM(FUNC) \ > > + ((u64)(FUNC) - (u64)__bpf_call_base) > > Looks good to me in general. My only concern is compilation on 32bit archs: I think > the cast needs to be of '(u64)(unsigned long)' to avoid introducing new warnings a la > 'cast from pointer to integer of different size'. Oh, good point. I'll double-check the 32-bit builds. (I also have another related change that I found several days later.) I'll get this adjusted/tested and resend the patch. Thanks! -Kees -- Kees Cook