From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_PASS, USER_AGENT_NEOMUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3958C43387 for ; Thu, 17 Jan 2019 01:02:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6E89120651 for ; Thu, 17 Jan 2019 01:02:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TIxtXIgH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727312AbfAQBCH (ORCPT ); Wed, 16 Jan 2019 20:02:07 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:35760 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725888AbfAQBCG (ORCPT ); Wed, 16 Jan 2019 20:02:06 -0500 Received: by mail-pl1-f193.google.com with SMTP id p8so3874342plo.2 for ; Wed, 16 Jan 2019 17:02:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ebQ+HqWHcBYj34UtVbjFijX50DzLKfujHjsExcBmtto=; b=TIxtXIgHEwYfLqKPSY5DWTAWhowYHSqRBcuJQePNTeUeE8bsqPuvi9Zyez9Y83clne IijacJTKNh1C3OZu+qpqWlXWVix0lJrOZpooP0gJqeTjhWw1Gk5k2Jb8V+r8SHcz7nc7 S0Lqm0xIlDzqHeCgnYFuYc0op60NFSIjg7KQpWYb01pjNySLENTPoVqdD2p6aY0mloG3 OpVzJuBhVN5x/U4lDTg5dJsEppdQYSBNK4sC4VL14fFtx22SP/ax4Yul9WjMxzBp0x08 zxODcrQwil39aOVGtJsJ/AHK6+gNqySPl9a3Tb43QlYBoPwsxeEk/S1jnkBdC/f1O4xJ zWNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ebQ+HqWHcBYj34UtVbjFijX50DzLKfujHjsExcBmtto=; b=Qs4ef8dRlbYpEY0oI4g/JyPJl2tHdYzWOE36xQ/HHocblbkihurl5FAvrmN7nuWh2f N+4K+vL8mLUQvocIDdjfC7aummVTRWbPiZivfpLrLfFwvxL8xtfNUTvURiUemD57I7wR LVYdIGN/UuRF8hPKy3dAOZpyFIwX1PJ+y6JC8wzSIWVg0KQUG2ruEly0MZoHpCH8D8Ov cKpnu3cV7uirYuprq0JXmoFD4HnB+OyoWe95uVq9M8It8jOMmcqo00woJ/Sp3KNOX/FM 36r/fkIIsXD72YOCyOBtJsWodt1AKZrpb6YrOkiL+/Qj2d2FqlbZc9A1KPRVg+2Hmi9O OG1w== X-Gm-Message-State: AJcUukeTYheawJNsK/P79luZy7cQGCkvVar8d9KIg84+zIz3NJ0Erfgm 6GzVv4pQN0nrQf4AiFuZwrA= X-Google-Smtp-Source: ALg8bN5VcwYjutU2FQlncqC10AiM9z+kOrQmabmZh8gsaI9QFJv0wBM/BdhJFR780obweCDV1s84qw== X-Received: by 2002:a17:902:2006:: with SMTP id n6mr13063155pla.66.1547686925582; Wed, 16 Jan 2019 17:02:05 -0800 (PST) Received: from ast-mbp ([2620:10d:c090:200::6:30ae]) by smtp.gmail.com with ESMTPSA id e128sm22506pfe.67.2019.01.16.17.02.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jan 2019 17:02:04 -0800 (PST) Date: Wed, 16 Jan 2019 17:02:03 -0800 From: Alexei Starovoitov To: Martin Lau Cc: Alexei Starovoitov , "davem@davemloft.net" , "daniel@iogearbox.net" , "jakub.kicinski@netronome.com" , "netdev@vger.kernel.org" , Kernel Team Subject: Re: [PATCH bpf-next 1/9] bpf: introduce bpf_spin_lock Message-ID: <20190117010201.dig6y6ddtfurzizt@ast-mbp> References: <20190116050830.1881316-1-ast@kernel.org> <20190116050830.1881316-2-ast@kernel.org> <20190117001615.pleazxhez7aplc3m@kafai-mbp.dhcp.thefacebook.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190117001615.pleazxhez7aplc3m@kafai-mbp.dhcp.thefacebook.com> User-Agent: NeoMutt/20180223 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Thu, Jan 17, 2019 at 12:16:18AM +0000, Martin Lau wrote: > On Tue, Jan 15, 2019 at 09:08:22PM -0800, Alexei Starovoitov wrote: > [ ... ] > > > +/* copy everything but bpf_spin_lock */ > > +static inline void copy_map_value(struct bpf_map *map, void *dst, void *src) > > +{ > > + if (unlikely(map_value_has_spin_lock(map))) { > > + u32 off = map->spin_lock_off; > > + > > + memcpy(dst, src, off); > > + memcpy(dst + off + sizeof(struct bpf_spin_lock), > > + src + off + sizeof(struct bpf_spin_lock), > > + map->value_size - off - sizeof(struct bpf_spin_lock)); > > + } else { > > + memcpy(dst, src, map->value_size); > > + } > > +} > > + > [ ... ] > > > +int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t) > > +{ > > + const struct btf_member *member; > > + u32 i, off = -ENOENT; > > + > > + if (BTF_INFO_KIND(t->info) != BTF_KIND_STRUCT) > > + return -EINVAL; > > + > > + for_each_member(i, t, member) { > > + const struct btf_type *member_type = btf_type_by_id(btf, > > + member->type); > > + if (!btf_type_is_struct(member_type)) > may be using "BTF_INFO_KIND(t->info) != BTF_KIND_STRUCT" here also. good point. will do. > > + continue; > > + if (member_type->size != sizeof(struct bpf_spin_lock)) > > + continue; > > + if (strcmp(__btf_name_by_offset(btf, member_type->name_off), > > + "bpf_spin_lock")) > > + continue; > > + if (off != -ENOENT) > > + /* only one 'struct bpf_spin_lock' is allowed */ > > + return -E2BIG; > > + off = btf_member_bit_offset(t, member); > > + if (off % 8) > > + /* valid C code cannot generate such BTF */ > > + return -EINVAL; > > + off /= 8; > > + if (off % __alignof__(struct bpf_spin_lock)) > > + /* valid struct bpf_spin_lock will be 4 byte aligned */ > > + return -EINVAL; > > + } > > + return off; > > +} > > + > [ ... ] > > > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > index b155cd17c1bd..ebf0a673cb83 100644 > > --- a/kernel/bpf/syscall.c > > +++ b/kernel/bpf/syscall.c > [ ... ] > > > err = security_bpf_map_alloc(map); > > @@ -740,7 +757,7 @@ static int map_lookup_elem(union bpf_attr *attr) > > err = -ENOENT; > > } else { > > err = 0; > > - memcpy(value, ptr, value_size); > > + copy_map_value(map, value, ptr); > copy_map_value() skips the bpf_spin_lock and "value" has not been zero-ed. > "value" is then copied to the "__user *uvalue". > May be init the bpf_spin_lock part of the "uvalue" to 0? I guess something went wrong with my scripts. The patch on my side has this: if (attr->flags & BPF_F_LOCK) { /* lock 'ptr' elem and copy * everything but the lock */ copy_map_value_locked(map, value, ptr, true); /* mask lock, since value was kmalloced */ check_and_init_map_lock(map, value); } else { copy_map_value(map, value, ptr); } and lock is inited to zero before copying to user space. But I don't see the same in patchworks, so you're absolutely right to point it out as a bug. > btw, somehow patch 6 and 7 are missing: > https://patchwork.ozlabs.org/cover/1025640/ Indeed and I cannot explain why. Hopefully v2 won't have this weirdness.