From patchwork Wed Dec 2 11:18:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 1409654 Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BXigPZQ+; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CmGjR43Nmz9sWD for ; Wed, 2 Dec 2020 22:20:27 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726987AbgLBLU0 (ORCPT ); Wed, 2 Dec 2020 06:20:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55886 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726024AbgLBLUZ (ORCPT ); Wed, 2 Dec 2020 06:20:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606907939; 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: content-transfer-encoding:content-transfer-encoding; bh=QCo/hwnFP7F9NRp+Ix7w3KjLN/Z5aQvlRSVmwPx1dTQ=; b=BXigPZQ+bFiBUq6xidHm9anfyg4xeJ4/UmqV1Ij9IQ3QD/9MsZTc9vSE3imC0uv0QW+2da +mSqrnAuip0Vmku/lZb312EVmA7+HW+agDNP+P3ZkpaCSYvWkCQg+7etEjnJpf3ruBmnPO ON6Qz3PfoJSbgoQT7KzV8ANBCKenqRs= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-219-wxnqPgIlNCCZw-w7Q0eXjw-1; Wed, 02 Dec 2020 06:18:57 -0500 X-MC-Unique: wxnqPgIlNCCZw-w7Q0eXjw-1 Received: by mail-qv1-f70.google.com with SMTP id i2so968491qvb.2 for ; Wed, 02 Dec 2020 03:18:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QCo/hwnFP7F9NRp+Ix7w3KjLN/Z5aQvlRSVmwPx1dTQ=; b=hYdP5s+v0jNzUD08lN3oAkyyc0mwkZOSCBxmYr0Fk/I54wX2jbxSFxpq9OWGH0NRWq 9klSOFyQWFgxLu91/4YVahP2ILTQ6GEaAdd4+RUtp8fplGSyyA2LbGJC340W4vrX2xIs TdIeazgStd7Qhe4wTHM1owhJ1PPA6K9tzv1OEymVU2IQAYsRT1ZPa+VgyOL+Mm7MH0q0 UQw6AccIxJRrLfwEDqY9QYDpls2fM296LGkepUEA+QKyb70rGdu/cxZhV15Fk3oqwu4+ v38QGgsBFyJM3Ltyv7LXqWECDlcgASPaRGHHv/IbL/c5ytR4m8w5nCfWE3f6+cKG0i3P 8arw== X-Gm-Message-State: AOAM53341S8zeARwb3peX8PmRxvjT8WXoo1cOr8DhrcBQHLw5kNY5/+g Nn8Of+iC2697OgtBKKMhphc+AB8wbuceJUhh0wlOggenHqrfJ4Z5w8699rL1LSvu+yB+pvhm7I+ QiCvevAGgaYfb2joR X-Received: by 2002:aed:38c8:: with SMTP id k66mr1988930qte.385.1606907936923; Wed, 02 Dec 2020 03:18:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzm8QvUSOY4z2LoSiHRtdlh4qfm/paFhlzdK975CsdHLH8f1GpJeutLM6U+2UmEZMz7HCiCWQ== X-Received: by 2002:aed:38c8:: with SMTP id k66mr1988886qte.385.1606907936455; Wed, 02 Dec 2020 03:18:56 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id u72sm1392306qka.15.2020.12.02.03.18.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2020 03:18:55 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 21BBE182EE9; Wed, 2 Dec 2020 12:18:54 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: daniel@iogearbox.net, ast@fb.com, andrii@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Toke_H=C3=B8iland?= =?utf-8?q?-J=C3=B8rgensen?= Subject: [PATCH bpf v2] libbpf: sanitise map names before pinning Date: Wed, 2 Dec 2020 12:18:16 +0100 Message-Id: <20201202111816.92279-1-toke@redhat.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When we added sanitising of map names before loading programs to libbpf, we still allowed periods in the name. While the kernel will accept these for the map names themselves, they are not allowed in file names when pinning maps. This means that bpf_object__pin_maps() will fail if called on an object that contains internal maps (such as sections .rodata). Fix this by replacing periods with underscores when constructing map pin paths. This only affects the paths generated by libbpf when bpf_object__ping_maps() is called with a path argument. Any pin paths set by bpf_map__set_pin_path() are unaffected, and it will still be up to the caller to avoid invalid characters in those. Fixes: 113e6b7e15e2 ("libbpf: Sanitise internal map names so they are not rejected by the kernel") Signed-off-by: Toke Høiland-Jørgensen --- v2: - Move string munging to helper function tools/lib/bpf/libbpf.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8d05132e1945..08ff7783fb93 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7651,6 +7651,20 @@ bool bpf_map__is_pinned(const struct bpf_map *map) return map->pinned; } +static char *sanitize_pin_path(char *str) +{ + char *s = str; + + /* bpffs disallows periods in path names */ + while (*s) { + if (*s == '.') + *s = '_'; + s++; + } + + return str; +} + int bpf_object__pin_maps(struct bpf_object *obj, const char *path) { struct bpf_map *map; @@ -7680,7 +7694,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path) err = -ENAMETOOLONG; goto err_unpin_maps; } - pin_path = buf; + pin_path = sanitize_pin_path(buf); } else if (!map->pin_path) { continue; } @@ -7724,7 +7738,7 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) return -EINVAL; else if (len >= PATH_MAX) return -ENAMETOOLONG; - pin_path = buf; + pin_path = sanitize_pin_path(buf); } else if (!map->pin_path) { continue; }