Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp461819img; Thu, 21 Mar 2019 01:51:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqx81nWBUc6qB5BndhYCRmM2zjMvOtykPPG1PtKE+s5Xb5p4rtD1aTKv2uFNYYx4O0XMa6CP X-Received: by 2002:a65:5c01:: with SMTP id u1mr2280216pgr.197.1553158287708; Thu, 21 Mar 2019 01:51:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553158287; cv=none; d=google.com; s=arc-20160816; b=wZlBzlPQJOGNpTpMtZiuNOFdnk4Tw1pSVfclpNKArraVGcMJAd6wJlyx1kswUmOHXN KoAUyr+UH5kNXa5bOYOwIOB0FuCkAYhcqvTRA2DhNkn90s73wGsTR99dEE1RBv9SXGza McQMq+Yxq28JIahcJlCdllqNXjDzFEkOZ2RQaJUdAxJ5UPKvMMDH4N0BLQR7hf423phq iWbhBQkMdrHdw3BN2HVjQ3C7H6juMEwh540MhIZA/uutjY05YeSpKbSpEZJwFtn+/+lk 1HrNvDgvMTMeLuORsX3oSCnBs6hTYQefQ3SwFOhlSt5alupIIr+3f/K9yCYNwYQ2v6o9 glDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject; bh=8fTZ2j8mNAJhqZ54PcpgMNZARY7Jx5Udf0H/yeySYC0=; b=m9S1Nc2qhAAWVrScuJ2jKJ44129ij1+jTI7x8cfGs5C9rGPh2/HdCDGnp/uLmAMSlO OyMWlxkQL0ETuRV6bAXPCFCrxEbPF15l6fc1nB3Lr7p3MSi0DXjiBouTAd4m5RDrgVRx Wn2bT20K2t5qXRQ2q1OYLbu6dSzOX0dqS5MDSEjv2msdoL39uweVqW35prn4uCYEdhlA aRszhHtdyLCQSMwAqFFmS3LmTZHL4E2lp2T5DC6+WFEv4q9KBasUaGcW7KmFyltgLo5c x7lEkKSRMYx6ofW/haCPw28s0LMjmCimi1W/8fiPr8p7uiXY+Lgz3EFSGdoKjUsghFMw P4bw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r4si3596956pgp.581.2019.03.21.01.51.12; Thu, 21 Mar 2019 01:51:27 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728257AbfCUIuH (ORCPT + 99 others); Thu, 21 Mar 2019 04:50:07 -0400 Received: from www62.your-server.de ([213.133.104.62]:59758 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727960AbfCUIuG (ORCPT ); Thu, 21 Mar 2019 04:50:06 -0400 Received: from [78.46.172.2] (helo=sslproxy05.your-server.de) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1h6tOm-00028l-OZ; Thu, 21 Mar 2019 09:50:04 +0100 Received: from [178.197.248.24] (helo=linux.home) by sslproxy05.your-server.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1h6tOm-000Rdr-Hu; Thu, 21 Mar 2019 09:50:04 +0100 Subject: Re: [PATCH] bpf: do not restore dst_reg when cur_state is freed To: Xu Yu , bpf@vger.kernel.org, linux-kernel@vger.kernel.org References: From: Daniel Borkmann Message-ID: Date: Thu, 21 Mar 2019 09:50:03 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.100.2/25394/Wed Mar 20 08:52:02 2019) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/21/2019 09:31 AM, Xu Yu wrote: > Syzkaller hit 'KASAN: use-after-free Write in sanitize_ptr_alu' bug. > Call trace: > dump_stack+0xbf/0x12e > print_address_description+0x6a/0x280 > kasan_report+0x237/0x360 > sanitize_ptr_alu+0x85a/0x8d0 > adjust_ptr_min_max_vals+0x8f2/0x1ca0 > adjust_reg_min_max_vals+0x8ed/0x22e0 > do_check+0x1ca6/0x5d00 > bpf_check+0x9ca/0x2570 > bpf_prog_load+0xc91/0x1030 > __se_sys_bpf+0x61e/0x1f00 > do_syscall_64+0xc8/0x550 > entry_SYSCALL_64_after_hwframe+0x49/0xbe > Fault injection trace: >  kfree+0xea/0x290 >  free_func_state+0x4a/0x60 >  free_verifier_state+0x61/0xe0 >  push_stack+0x216/0x2f0 <- inject failslab >  sanitize_ptr_alu+0x2b1/0x8d0 >  adjust_ptr_min_max_vals+0x8f2/0x1ca0 >  adjust_reg_min_max_vals+0x8ed/0x22e0 >  do_check+0x1ca6/0x5d00 >  bpf_check+0x9ca/0x2570 >  bpf_prog_load+0xc91/0x1030 >  __se_sys_bpf+0x61e/0x1f00 >  do_syscall_64+0xc8/0x550 >  entry_SYSCALL_64_after_hwframe+0x49/0xbe > > When kzalloc() fails in push_stack(), free_verifier_state() will free > current verifier state. As push_stack() returns, dst_reg was restored > if ptr_is_dst_reg is false. However, as member of the cur_state, dst_reg > is also freed, and error occurs when dereferencing dst_reg. > > Simply fix it by checking whether cur_state is NULL before retoring > dst_reg. > > Signed-off-by: Xu Yu > --- > kernel/bpf/verifier.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index ce166a0..018ce4f 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -3368,7 +3368,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env, > *dst_reg = *ptr_reg; > } > ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); > - if (!ptr_is_dst_reg) > + if (!ptr_is_dst_reg && env->cur_state) > *dst_reg = tmp; Good catch, test should be more obvious rewritten as: if (!ptr_is_dst_reg && ret) Could you resubmit with that? > return !ret ? -EFAULT : 0; > } > Thanks, Daniel