Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4170966pxb; Mon, 1 Feb 2021 14:36:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+lPRkXYapZ7fyt+Y6zhQpIzjj60PFgZoTAjsMBdswGpV3Zznnv7zI2G2voTWZLJD9nON0 X-Received: by 2002:a17:906:af41:: with SMTP id ly1mr11202504ejb.525.1612218975172; Mon, 01 Feb 2021 14:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612218975; cv=none; d=google.com; s=arc-20160816; b=I1MCftwJm08r/ElV6NBRfQGLkbg45zCTCXuNKaw7a0MbPjqzxNIH9HDpDIoOQt8V2H sBQZ6BokrcN0D6xmJhpjAc6mQotst9NKSzBY1lQUzkle+yfpCW5ouQpmUkTAMoSrgqwi ZgB116keO9X7j8IQOrkSFAiw7cXHrqren6lOgslUelVup3k8dTTFe96UTC//Ef+DyuqC yxk+INBHOnC1YkU7b0WO7h0mpwAtsx1RqmxIPPzPQ/ObP5YA7Ez+6zwfgNo04wYsoQsd BhSKDdCeoY5TiUDPAlNKnf7qXD6CbAgOXFUNdLtQ7QZszSiF+xE/dLwJdtw/PsK4QCZT THyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=DxmVPH4IbBIxLnO2J+bywnfvnOhjx9o1eJ/SGTtWITo=; b=txc5rCVUwduUY6RHlLJ8PTd3o0tP5kJ3ENfkDdNi5gQ7GVNNH1BHTCF3cMR+lpIC08 zDQEb5KZQj919I+NWgrjRJNWF4YqBLdDeOHiQq3AQatloOvIwQOILbyzGuCzrDLTog3O JRru63vv+tB8fvGppFEy555BMRp/amP0ZDDWD6VFp4UQDBUBnHu4yUuOaGOApJDLDaU7 LT5KeNA1aIFI3SUgknDww7ufo7j79WBr5pO7H+fepKSqfidPkj92W3kq1xZ1IpaHs1pJ 2opjZFei9ExjtBcqTdSwrTO6wbJzpJlU+1atEAA7Oh6vaMIQWgQ2pnO7H1IeDtJ2GmuM M9nQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dx12si5414220ejb.547.2021.02.01.14.35.50; Mon, 01 Feb 2021 14:36:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229537AbhBAWcs (ORCPT + 99 others); Mon, 1 Feb 2021 17:32:48 -0500 Received: from www62.your-server.de ([213.133.104.62]:42036 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbhBAWcq (ORCPT ); Mon, 1 Feb 2021 17:32:46 -0500 Received: from sslproxy01.your-server.de ([78.46.139.224]) by www62.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1l6hjn-000GL3-1d; Mon, 01 Feb 2021 23:32:03 +0100 Received: from [85.7.101.30] (helo=pc-9.home) by sslproxy01.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l6hjm-0008TG-PV; Mon, 01 Feb 2021 23:32:02 +0100 Subject: Re: [PATCH bpf-next v6 2/5] bpf: Expose bpf_get_socket_cookie to tracing programs To: Florent Revest Cc: Andrii Nakryiko , bpf , Alexei Starovoitov , Andrii Nakryiko , KP Singh , open list , KP Singh , john.fastabend@gmail.com, yhs@fb.com References: <20210126183559.1302406-1-revest@chromium.org> <20210126183559.1302406-2-revest@chromium.org> <4a8ceab1-6eef-9fda-0502-5a0550f53ddc@iogearbox.net> <37730136-2c33-589c-a749-4221b60b9751@iogearbox.net> From: Daniel Borkmann Message-ID: <3f850e85-72ee-5a69-a6f4-7a2daab3af67@iogearbox.net> Date: Mon, 1 Feb 2021 23:32:01 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.102.4/26067/Mon Feb 1 13:25:31 2021) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/30/21 12:45 PM, Florent Revest wrote: > On Fri, Jan 29, 2021 at 1:49 PM Daniel Borkmann wrote: >> On 1/29/21 11:57 AM, Daniel Borkmann wrote: >>> On 1/27/21 10:01 PM, Andrii Nakryiko wrote: >>>> On Tue, Jan 26, 2021 at 10:36 AM Florent Revest wrote: >>>>> >>>>> This needs a new helper that: >>>>> - can work in a sleepable context (using sock_gen_cookie) >>>>> - takes a struct sock pointer and checks that it's not NULL >>>>> >>>>> Signed-off-by: Florent Revest >>>>> Acked-by: KP Singh >>>>> --- >>>>> include/linux/bpf.h | 1 + >>>>> include/uapi/linux/bpf.h | 8 ++++++++ >>>>> kernel/trace/bpf_trace.c | 2 ++ >>>>> net/core/filter.c | 12 ++++++++++++ >>>>> tools/include/uapi/linux/bpf.h | 8 ++++++++ >>>>> 5 files changed, 31 insertions(+) >>>>> >>>>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h >>>>> index 1aac2af12fed..26219465e1f7 100644 >>>>> --- a/include/linux/bpf.h >>>>> +++ b/include/linux/bpf.h >>>>> @@ -1874,6 +1874,7 @@ extern const struct bpf_func_proto bpf_per_cpu_ptr_proto; >>>>> extern const struct bpf_func_proto bpf_this_cpu_ptr_proto; >>>>> extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto; >>>>> extern const struct bpf_func_proto bpf_sock_from_file_proto; >>>>> +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto; >>>>> >>>>> const struct bpf_func_proto *bpf_tracing_func_proto( >>>>> enum bpf_func_id func_id, const struct bpf_prog *prog); >>>>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h >>>>> index 0b735c2729b2..5855c398d685 100644 >>>>> --- a/include/uapi/linux/bpf.h >>>>> +++ b/include/uapi/linux/bpf.h >>>>> @@ -1673,6 +1673,14 @@ union bpf_attr { >>>>> * Return >>>>> * A 8-byte long unique number. >>>>> * >>>>> + * u64 bpf_get_socket_cookie(void *sk) >>>> >>>> should the type be `struct sock *` then? >>> >>> Checking libbpf's generated bpf_helper_defs.h it generates: >>> >>> /* >>> * bpf_get_socket_cookie >>> * >>> * If the **struct sk_buff** pointed by *skb* has a known socket, >>> * retrieve the cookie (generated by the kernel) of this socket. >>> * If no cookie has been set yet, generate a new cookie. Once >>> * generated, the socket cookie remains stable for the life of the >>> * socket. This helper can be useful for monitoring per socket >>> * networking traffic statistics as it provides a global socket >>> * identifier that can be assumed unique. >>> * >>> * Returns >>> * A 8-byte long non-decreasing number on success, or 0 if the >>> * socket field is missing inside *skb*. >>> */ >>> static __u64 (*bpf_get_socket_cookie)(void *ctx) = (void *) 46; >>> >>> So in terms of helper comment it's picking up the description from the >>> `u64 bpf_get_socket_cookie(struct sk_buff *skb)` signature. With that >>> in mind it would likely make sense to add the actual `struct sock *` type >>> to the comment to make it more clear in here. >> >> One thought that still came to mind when looking over the series again, do >> we need to blacklist certain functions from bpf_get_socket_cookie() under >> tracing e.g. when attaching to, say fexit? For example, if sk_prot_free() >> would be temporary uninlined/exported for testing and bpf_get_socket_cookie() >> was invoked from a prog upon fexit where sock was already passed back to >> allocator, I presume there's risk of mem corruption, no? > > Mh, this is interesting. I can try to add a deny list in v7 but I'm > not sure whether I'll be able to catch them all. I'm assuming that > __sk_destruct, sk_destruct, __sk_free, sk_free would be other > problematic functions but potentially there would be more. I was just looking at bpf_skb_output() from a7658e1a4164 ("bpf: Check types of arguments passed into helpers") which afaiu has similar issue, back at the time this was introduced there was no fentry/fexit but rather raw tp progs, so you could still safely dump skb this way including for kfree_skb() tp. Presumably if you bpf_skb_output() at 'fexit/kfree_skb' you might be able to similarly crash your kernel which I don't think is intentional (also given we go above and beyond in other areas to avoid crashing or destabilizing e.g. [0] to mention one). Maybe these should really only be for BPF_TRACE_RAW_TP (or BPF_PROG_TYPE_LSM) where it can be audited that it's safe to use like a7658e1a4164's original intention ... or have some sort of function annotation like __acquires/__releases but for tracing e.g. __frees(skb) where use would then be blocked (not sure iff feasible). [0] https://lore.kernel.org/bpf/20210126001219.845816-1-yhs@fb.com/