Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp76984iof; Sun, 5 Jun 2022 21:38:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpDVuWCfzjK98PYRt1EFffEONSxOnOe366VFAEDtKL1/hnxO9C69+tAAv5UBQM3DWqjqaW X-Received: by 2002:a62:1c8b:0:b0:51b:608f:7dff with SMTP id c133-20020a621c8b000000b0051b608f7dffmr22592204pfc.1.1654490314151; Sun, 05 Jun 2022 21:38:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654490314; cv=none; d=google.com; s=arc-20160816; b=wv1fv7sm43T9b5tgspTYrd6H0zDhgZM/cWdaXiUPMDPAqTojcypGypMFrXuzeh8FbD S1SvlE5B0iLUb7a6oQu/2pPSK4XhCEbkMknR5CMU9CNAb1jMWcZysPKOxH/QXXsBkn9T 9WbNRxZcZ7P7muiElhmVyqdyI6oMctcqAA4nkDgLB6ZhJ9dtwDNQAIB3poPzTpWQBt57 48VXee+/HtFjx2hrRUBnLVQ0KVahxRVBAK0LiMaV4qoLcog2uWGqa3E3EybnsfixmvTf fGnjn1Pym5XFYx6v/v9G72S+oP1Ky8IqqUzuF2crISg2FVVAPUDIDu5E0oAco8A90KU2 2CZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=UrSyfjfeqVZrZfHKJrmVADtzpO1c1GhQspOF8ytnTY4=; b=qZf60xVyzaiORwWxvZrK1wU3zF1DEsnUNrqk64lhLd8EL3dgAZh9pNF71osKkhNk+k mMM3Cwm7DeplgZB0PYmHoflDkOS1kEh5NM3SkOQ7eeBmrGDIUWdcwLnwYLUbBj5+CzgP /Cx5wP5VzARaPGx5+M28XhuAfJ/8maV6RpzViy5AaBsRLm2AIaVZ9chuW1xKyFIffb+B iEDldBBS+ML4H6CAJK4GNBbrekNCAygAVfvg0y2UuCuHTKLPcer/0bE3pN9qgHLCGyv1 yh+C8pLt+rw2Ni8SC5Y97TXQVqDFQcx+VaAecbxaa6B4PpwX5ZWbcYuwPXN9afokKkpx bS/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jCII96mh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c11-20020a056a000acb00b0051b2c6fe811si20428663pfl.90.2022.06.05.21.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 21:38:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jCII96mh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AD49B9A99A; Sun, 5 Jun 2022 21:01:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239862AbiFBXBZ (ORCPT + 99 others); Thu, 2 Jun 2022 19:01:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229921AbiFBXBV (ORCPT ); Thu, 2 Jun 2022 19:01:21 -0400 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A2372872E; Thu, 2 Jun 2022 16:01:20 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id q1so6759358ljb.5; Thu, 02 Jun 2022 16:01:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UrSyfjfeqVZrZfHKJrmVADtzpO1c1GhQspOF8ytnTY4=; b=jCII96mhcASnjxlAKJgImX/8mKQeIsGCBCzykznSe2UfsOz1SzzHQ74GA1CDd1tppr /qF3jwdKYrrSpx/g9rUTEH2Y35B3Oypto/Uuu3hthBV1tb4Sf9A/so3ZfTmaKspJ0gkg mkHjP3v28HTfIriisu3ZJhDdGJxRFCH1jfMOM4dHLxG4bJhUJtQHJ+UyvPljL147JoFL fXdNTMmxt8YjZ9kXCPYCkMcqVPySUq/88vMl9BV9z65NbZzfNlCE+Jx7SqG+oltuvigg fM3TLh82F9i7q3btI9jDd0RR1qiwDt53RaqGetVRQN5ES8ZLxzypqGTry4nz19pZ0wKx glCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UrSyfjfeqVZrZfHKJrmVADtzpO1c1GhQspOF8ytnTY4=; b=FjSIQhRoGDkQQAFp+dtzBbLM0QV2/2vYKKzbIM6qVWOkGrDpMppBneBKHZ1qGbrdNL GYrAgHlW0vray07I91kyHiP8blrJtVaMRnxx4AJvXmQaMGarK/L5m1TgQyvTLHi1S0WO KLxPbz0XlJkUprRFGmLiyfHsKHZcGVD5iu5c1Sj2rtos55hTDDa2QHjFfDV3wJo1+iji svu3qF1UnBIQaxWJOuJQXeNSfG1K/6K2ZgZNhjTO0qKQ0/IFmJ1IyXj+97qnUZbnTWr0 F8Nb722cY0349iOqpAwf2NURq8Q2vVA+/jcfBSyudbFWNQDPSEMLDPZ8itu+8+ajTqbs fyTw== X-Gm-Message-State: AOAM530V8lD3UJki4EMctxjmJnFnSNQyyBzOxQ2DMmlk1MoadgueWb98 A7V0GVBpmybmansLFg9sqKTE8NUUyPHOa6CvvcA= X-Received: by 2002:a2e:9bc1:0:b0:253:e20a:7a79 with SMTP id w1-20020a2e9bc1000000b00253e20a7a79mr36466327ljj.445.1654210878588; Thu, 02 Jun 2022 16:01:18 -0700 (PDT) MIME-Version: 1.0 References: <20220527205611.655282-1-jolsa@kernel.org> <20220527205611.655282-4-jolsa@kernel.org> In-Reply-To: <20220527205611.655282-4-jolsa@kernel.org> From: Andrii Nakryiko Date: Thu, 2 Jun 2022 16:01:07 -0700 Message-ID: Subject: Re: [PATCH bpf-next 3/3] bpf: Force cookies array to follow symbols sorting To: Jiri Olsa Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Masami Hiramatsu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Fri, May 27, 2022 at 1:57 PM Jiri Olsa wrote: > > When user specifies symbols and cookies for kprobe_multi link > interface it's very likely the cookies will be misplaced and > returned to wrong functions (via get_attach_cookie helper). > > The reason is that to resolve the provided functions we sort > them before passing them to ftrace_lookup_symbols, but we do > not do the same sort on the cookie values. > > Fixing this by using sort_r function with custom swap callback > that swaps cookie values as well. > > Fixes: 0236fec57a15 ("bpf: Resolve symbols with ftrace_lookup_symbols for kprobe multi link") > Signed-off-by: Jiri Olsa > --- > kernel/trace/bpf_trace.c | 65 ++++++++++++++++++++++++++++++---------- > 1 file changed, 50 insertions(+), 15 deletions(-) > > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index 10b157a6d73e..e5c423b835ab 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -2423,7 +2423,12 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip, > kprobe_multi_link_prog_run(link, entry_ip, regs); > } > > -static int symbols_cmp(const void *a, const void *b) > +struct multi_symbols_sort { > + const char **funcs; > + u64 *cookies; > +}; > + > +static int symbols_cmp_r(const void *a, const void *b, const void *priv) > { > const char **str_a = (const char **) a; > const char **str_b = (const char **) b; > @@ -2431,6 +2436,25 @@ static int symbols_cmp(const void *a, const void *b) > return strcmp(*str_a, *str_b); > } > > +static void symbols_swap_r(void *a, void *b, int size, const void *priv) > +{ > + const struct multi_symbols_sort *data = priv; > + const char **name_a = a, **name_b = b; > + u64 *cookie_a, *cookie_b; > + > + cookie_a = data->cookies + (name_a - data->funcs); > + cookie_b = data->cookies + (name_b - data->funcs); > + > + /* swap name_a/name_b and cookie_a/cookie_b values */ > + swap(*name_a, *name_b); > + swap(*cookie_a, *cookie_b); > +} > + > +static int symbols_cmp(const void *a, const void *b) > +{ > + return symbols_cmp_r(a, b, NULL); > +} > + > int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) > { > struct bpf_kprobe_multi_link *link = NULL; > @@ -2468,6 +2492,19 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr > if (!addrs) > return -ENOMEM; > > + ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies); > + if (ucookies) { > + cookies = kvmalloc(size, GFP_KERNEL); > + if (!cookies) { > + err = -ENOMEM; > + goto error; > + } > + if (copy_from_user(cookies, ucookies, size)) { > + err = -EFAULT; > + goto error; > + } > + } > + > if (uaddrs) { > if (copy_from_user(addrs, uaddrs, size)) { > err = -EFAULT; > @@ -2480,26 +2517,24 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr > if (err) > goto error; > > - sort(us.syms, cnt, sizeof(*us.syms), symbols_cmp, NULL); > + if (cookies) { > + struct multi_symbols_sort data = { > + .cookies = cookies, > + .funcs = us.syms, > + }; > + > + sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r, > + symbols_swap_r, &data); > + } else { > + sort(us.syms, cnt, sizeof(*us.syms), symbols_cmp, NULL); > + } maybe just always do sort_r, swap callback can just check if cookie array is NULL and if not, additionally swap cookies? why have all these different callbacks and complicate the code unnecessarily? > + > err = ftrace_lookup_symbols(us.syms, cnt, addrs); > free_user_syms(&us); > if (err) > goto error; > } > > - ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies); > - if (ucookies) { > - cookies = kvmalloc(size, GFP_KERNEL); > - if (!cookies) { > - err = -ENOMEM; > - goto error; > - } > - if (copy_from_user(cookies, ucookies, size)) { > - err = -EFAULT; > - goto error; > - } > - } > - > link = kzalloc(sizeof(*link), GFP_KERNEL); > if (!link) { > err = -ENOMEM; > -- > 2.35.3 >