Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3294257iob; Mon, 16 May 2022 18:26:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCj3K9wX2CWYMCr6aeQSWAZmbWbxQ1LBbPo17K/Eb4Fof/hiN8PN9VdliMbwGAq28h5B3T X-Received: by 2002:a17:907:1c92:b0:6fa:51d2:4005 with SMTP id nb18-20020a1709071c9200b006fa51d24005mr17642117ejc.307.1652750770280; Mon, 16 May 2022 18:26:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652750770; cv=none; d=google.com; s=arc-20160816; b=mD67P06u0dtZ7nlLxjyy+dKv6aTYLpU9lWoG7OEpxGrj/2ulpXaEvv3u9xndrC0NaM 2yMySlS5MTfl/IRteFC5sm31spv/0p89vJkq6tC+Lpyb7F/FfVDbL+gqD0Z2RiunYnUQ 0GgfikIQEkGR6PwEe0FaB4/hI6fYcR0JyUQNuazpzl4AGZqr0QNouz2vrS3oUS6s9yj8 b8Wqpg24ZrrA1JE2dJZjFNeX5fRIxq2VcqyWapXBy/FS0l/CjR3kDdQsAcUh/B26XUmD FkrblMJ4r4xjEPPcro9XC3YlLXegI8vM+frL82oKXNqOenDK9KRT4E9k5s44Hi3v43Gj Suhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=CY4ff1BnqapePMK07WNltTRPHXz8albFraThTnxLBT4=; b=NpXXfqMX4qVbbgHWkv5UbQaujZdbWzmo8l5yUfOg59CCvYDPtvms+wR3WsyHbMhwU4 T+lb4jnTQ9kPfPryGrz4VmBC9ojkSgvZxGb/xpqgAvOUo7HkBbFS3fvW1A45uTLhMcbT txg/zAXVRH09dQIIcXsXQBZqcvzvf7Fck+W9lj9SnwLvOOryp90/AuhzTGUXV61ctv03 FbgVnSmxYg+gjTofCZjnnTRGDHYr2lD8B8xFtOQT00epa7ZWtA1fYt8ri7vlLqp57pZv eVJ+DzcRbAOCiOGdESxmoD5QlASYZ3t4VO+FOi/WSOdSePRyd0IhSMZHl5igyUwYb4DO PHBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AbTatxnK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg27-20020a170907971b00b006f457f90cd9si1033644ejc.300.2022.05.16.18.25.45; Mon, 16 May 2022 18:26:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AbTatxnK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344825AbiEPS2L (ORCPT + 99 others); Mon, 16 May 2022 14:28:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344848AbiEPS2B (ORCPT ); Mon, 16 May 2022 14:28:01 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A22423E0FE for ; Mon, 16 May 2022 11:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652725673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=CY4ff1BnqapePMK07WNltTRPHXz8albFraThTnxLBT4=; b=AbTatxnKuu6B0pR2oHBhj5u9LUSdmZSX9bLxQ0OMeXofoPd5Vl2AbZSy/r3bk97wFNe2y9 so4o0ldrkDfrpF4YE1sp22AtHVZ8xjK4flErZ9vgT587zRk7MdHOsPf7ay5Lp/csQlQJSj /0CMbt35aY1VWhfthdxM1pV4tth8dk8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-170-30w22ZIzOYuu65uh1XF7Sw-1; Mon, 16 May 2022 14:27:47 -0400 X-MC-Unique: 30w22ZIzOYuu65uh1XF7Sw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7EFAE100BAA0; Mon, 16 May 2022 18:27:46 +0000 (UTC) Received: from asgard.redhat.com (unknown [10.36.110.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BA1BD1121314; Mon, 16 May 2022 18:27:42 +0000 (UTC) Date: Mon, 16 May 2022 20:27:40 +0200 From: Eugene Syromiatnikov To: Jiri Olsa , Masami Hiramatsu , Steven Rostedt , Ingo Molnar , Alexei Starovoitov , Daniel Borkmann Cc: Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH bpf 3/4] bpf_trace: handle compat in kprobe_multi_resolve_syms Message-ID: <20220516182740.GA29979@asgard.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 For compat processes, userspace pointer size is different. Since the copied array is iterated anyway, the simplest fix seems to be copy the user-supplied array as-is and the iterate as an array of native or compat pointers, depending on the in_compat_syscall() value. Fixes: 0dcac272540613d4 ("bpf: Add multi kprobe link") Signed-off-by: Eugene Syromiatnikov --- kernel/trace/bpf_trace.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index d228440..5b0cf54 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2352,16 +2352,21 @@ static int kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt, unsigned long *addrs) { - unsigned long addr, size; + unsigned long addr; + size_t sym_size; + u32 size, elem_size; const char __user **syms; + compat_uptr_t __user *compat_syms; int err = -ENOMEM; unsigned int i; char *func; - if (check_mul_overflow(cnt, sizeof(*syms), &size)) + elem_size = in_compat_syscall() ? sizeof(*compat_syms) : sizeof(*syms); + if (check_mul_overflow(cnt, elem_size, &size)) return -EOVERFLOW; - size = cnt * sizeof(*syms); + size = cnt * elem_size; syms = kvzalloc(size, GFP_KERNEL); + compat_syms = (void *)syms; if (!syms) return -ENOMEM; @@ -2375,7 +2380,10 @@ kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt, } for (i = 0; i < cnt; i++) { - err = strncpy_from_user(func, syms[i], KSYM_NAME_LEN); + const char __user *ufunc = in_compat_syscall() + ? (char __user *)(uintptr_t)compat_syms[i] + : syms[i]; + err = strncpy_from_user(func, ufunc, KSYM_NAME_LEN); if (err == KSYM_NAME_LEN) err = -E2BIG; if (err < 0) @@ -2384,9 +2392,9 @@ kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt, addr = kallsyms_lookup_name(func); if (!addr) goto error; - if (!kallsyms_lookup_size_offset(addr, &size, NULL)) + if (!kallsyms_lookup_size_offset(addr, &sym_size, NULL)) goto error; - addr = ftrace_location_range(addr, addr + size - 1); + addr = ftrace_location_range(addr, addr + sym_size - 1); if (!addr) goto error; addrs[i] = addr; -- 2.1.4