Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp120952iof; Sun, 5 Jun 2022 23:00:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFIHmmB98YmFU52EQOicum0Pf10bqDz43OGJKtZraVk488CaCn667dHELKXeQDgLKCiatW X-Received: by 2002:a17:902:ce8f:b0:163:cc85:ba89 with SMTP id f15-20020a170902ce8f00b00163cc85ba89mr22076413plg.79.1654495235127; Sun, 05 Jun 2022 23:00:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495235; cv=none; d=google.com; s=arc-20160816; b=ybaeghe1BW4bLfhpqTXYsaHS7RvbOzFV4Kn0a+ZM0tkM7zyXpHEVGfRf4bihRfEzk0 YeEaws5HJecD1WXcthArxaddyv6g0rNr3+4gIThe6DbSaCV91UPCLCOdn7NhUomKtKb0 fZ5mpCPwYp5BMzyR3vdBrCOBaKlQbUt72hAvPP146SLlUCgp2BHcR5KrxacXYfUhpzbA +bNjIbMw6CKmafF9TWBoIqNfWRoRkyzZtCVVBuO2GRJ650a8zXFYXPbUKKO55qQtihYS ziYzklq/QTDcv873jWj+fPpWnwdU48ByOYXc1MVWXcrn7qKmBABIwqDvvaNaTME0MLfD s1Gw== 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=QjV2keLn8ksFTBP7+D6RPrmgrPXRhau2LX6VaRt9vA8=; b=lliqCF5ABC2B62VdyROmUujDKoTZil6bzgJn7RqUXwddoxNQE8VXLW+2PZQv8rSaDG mk/emezm4WoMSQJIHl5mjXIw8TAWSjy+RfuQ3eg3grZWtcEsSRHYIOGmiP9ATidQPZdX SXtY8kFJWNsiOci6RvLJ5c2dNj2sMN08EttCOECYaYgNoEjs+98oqgWTbNZdjxplGUK/ dV85EuMvl3rznrHlHSEEXTlelHK6puwfABLG8tHq+nzo7zuar2iOZ+qGcyvKj6iCNK6n I42kuEedlVOq9t1Ygiiv3146EkUxvn2vEK4UHiOtYdbDOoUu5c3Mmkxvm+T9CTySEJk8 Vkkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ahTiww37; 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 145-20020a630597000000b003f5f0a397e6si20196215pgf.814.2022.06.05.23.00.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:00:35 -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=ahTiww37; 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 668CF2FB424; Sun, 5 Jun 2022 21:48:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349106AbiFCV4o (ORCPT + 99 others); Fri, 3 Jun 2022 17:56:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231860AbiFCV4l (ORCPT ); Fri, 3 Jun 2022 17:56:41 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB56424967; Fri, 3 Jun 2022 14:56:39 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id y15so4361409ljc.0; Fri, 03 Jun 2022 14:56:39 -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=QjV2keLn8ksFTBP7+D6RPrmgrPXRhau2LX6VaRt9vA8=; b=ahTiww370kDspuCZq2eTYtjPaNCKKAYGHHg5ucqUyee3trD7vZ4sy38hO4w4XvNxn0 nw3LQG3eJ9TOrOSXraJC5qz0Pn7U8s+kglX803z3Bu67tkGUvlF55iQzSoycFT5UNuxh 9pVRKG+b3G9QUhE9CAkToEIeE7DioG1fvXyl99zTeOdFpG9E01iCJUN3sZUER9VGu1KQ wIJ3HxerC3GZyeQASwrzCYQwENgq8zgqJ+4b69e2it/17pPKO/aJAPBzK7J4vG6Kk10k HALlrz846mahAlJwvoBd3CqYLNB9KmHvPBxJODBZ5KBvMoceg/8E1ESHi+9gOcHgGIJu WhQA== 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=QjV2keLn8ksFTBP7+D6RPrmgrPXRhau2LX6VaRt9vA8=; b=XoMUHHVqt7aZhApjG3xFTy5jU2brhqmcY5p5nggKunqbLw8K00+oiNE4/vBcxeRPDa LMXJJtN2QZAmUPcVaxeV+yQL9oL3RqyGEAeK4lv+jaRTCCNkBeT+oZ/jGYQnGvszJ7am HeVnm41tXmHLoDut1KYE+elFBayuqjaK4DMild2KUq0DEK8NuF6tvmK8eKaXYYH38Mdt c2RU7k0hmiK6uGFK7TW+20wvzeSOmOu3R3cvaOfNB/MQuZftn/fIztXQmOR52Cpfl9hl /tvUKTAI3sRmxK/8QFJT8a3+b+qavdR2auWZbIpXi6fagKGFIQjsAkY1PFkW8AOfpr9J c61g== X-Gm-Message-State: AOAM533b8X0pusxQ5Nk1L6qeUoZo3cekV3000M4Va1KJ9Ju0RD8/gEuX sc/mGEtgJYjuBJ49iM4ZgAuhl8PS0WU7Fgw3g30= X-Received: by 2002:a2e:87c8:0:b0:255:6d59:ebce with SMTP id v8-20020a2e87c8000000b002556d59ebcemr7700808ljj.455.1654293398272; Fri, 03 Jun 2022 14:56:38 -0700 (PDT) MIME-Version: 1.0 References: <20220527205611.655282-1-jolsa@kernel.org> <20220527205611.655282-4-jolsa@kernel.org> In-Reply-To: From: Andrii Nakryiko Date: Fri, 3 Jun 2022 14:56:26 -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, Jun 3, 2022 at 3:23 AM Jiri Olsa wrote: > > On Thu, Jun 02, 2022 at 04:02:28PM -0700, Andrii Nakryiko wrote: > > On Thu, Jun 2, 2022 at 4:01 PM Andrii Nakryiko > > wrote: > > > > > > 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); > > > > oh, and you'll have to rebase anyways after kvmalloc_array patch > > true, that kvmalloc_array change went to bpf-next/master, > but as Song mentioned this patchset should probably go for bpf/master? > > I'm fine either way, let me know ;-) > I've moved kvmalloc_array() fix to bpf tree (it is an actual fix against potential overflow after all), so please base everything on bpf tree. > thanks, > jirka