From patchwork Mon Nov 30 16:17:20 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: 11940975 X-Patchwork-Delegate: bpf@iogearbox.net 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=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 53BF7C64E8A for ; Mon, 30 Nov 2020 16:19:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AD432206E3 for ; Mon, 30 Nov 2020 16:19:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GdJcj7BA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728730AbgK3QS5 (ORCPT ); Mon, 30 Nov 2020 11:18:57 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:53736 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727243AbgK3QS5 (ORCPT ); Mon, 30 Nov 2020 11:18:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606753051; 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=1WGnk9TNl9hRGZTaPRBMI0GW5RawwawDmOTeaOs/1fE=; b=GdJcj7BA2JtxPwgLx5MWde6JWgEbSDal2F11Gziigh6qCOT4wMqJwyIP+EGpcQBqxsWctO 797i001sYkoF/PM5hoJT2WyHNmBc5T2lxS78zvyAYjhBqCm6t/TrDgC/O1cMUpJpcC3RU8 G94KBzGEt/q0luUcQJIfUr8OXR46fuA= 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-8-ApNldyzbPX2enMYmiSESEg-1; Mon, 30 Nov 2020 11:17:28 -0500 X-MC-Unique: ApNldyzbPX2enMYmiSESEg-1 Received: by mail-qv1-f70.google.com with SMTP id i11so7831942qvo.11 for ; Mon, 30 Nov 2020 08:17:28 -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=1WGnk9TNl9hRGZTaPRBMI0GW5RawwawDmOTeaOs/1fE=; b=c+mdbD4CmtTEXb+tDmgZhoIWbBcVDernTE8fITKo1dZZrOC+dAQ73V5J/5eevUEgHl QrqcqP7s2v1i9KpNtsB+zwz7S8to1zQWEUyXYgkFJLJnLrbANnuT8ZWcGgHmB7UGjNH8 HXj2pcRLrFo2VlSsQ2gEkWpUP+B3E7zE2bcFtch18E5WqhzmUYs50ESSUREjGHGieAe2 kg3/AoHEwfH2dyqytlkDFWfQYciA03pLmJzQKx1JrUgBh39I2j4azDeRdBr1d3eXBhOO 4uloOgUbohzJk0WFQJTG37ekw6U1f0fRu7/P1Hnwy5brmrzTuL24aSNnomlnhWuDAIDx fvrw== X-Gm-Message-State: AOAM5318dxvbtxwphlor2cOmoTpsTxDwxVQX6f2BG/QjNtFpI5O6Rk9X qPcc7NINStw++M2fJPXMTprkDL+26C8TW2mG0S3RuR02rcCYAqPvCDjoWbGcUrzpeWVF3nNAkWn dg1iv7b2svLNl X-Received: by 2002:a05:620a:15f7:: with SMTP id p23mr22454886qkm.98.1606753047923; Mon, 30 Nov 2020 08:17:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwnsEcJmK+FERRj9g5fK0zGHU+Cfr4E6egU60uQNMvh2OoTO0vjyU5FpRzYgNxc7OCNct/49w== X-Received: by 2002:a05:620a:15f7:: with SMTP id p23mr22454818qkm.98.1606753047315; Mon, 30 Nov 2020 08:17:27 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id h13sm17770477qtc.4.2020.11.30.08.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2020 08:17:26 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id D9570181AD4; Mon, 30 Nov 2020 17:17:24 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: daniel@iogearbox.net, ast@fb.com, andrii@kernel.org Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf] libbpf: sanitise map names before pinning Date: Mon, 30 Nov 2020 17:17:20 +0100 Message-Id: <20201130161720.8688-1-toke@redhat.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- tools/lib/bpf/libbpf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8d05132e1945..8a3b4713b356 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7665,8 +7665,8 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path) } bpf_object__for_each_map(map, obj) { + char buf[PATH_MAX], *s = buf; char *pin_path = NULL; - char buf[PATH_MAX]; if (path) { int len; @@ -7680,6 +7680,8 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path) err = -ENAMETOOLONG; goto err_unpin_maps; } + while ((s = strstr(s, "."))) + *s = '_'; pin_path = buf; } else if (!map->pin_path) { continue; @@ -7712,8 +7714,8 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) return -ENOENT; bpf_object__for_each_map(map, obj) { + char buf[PATH_MAX], *s = buf; char *pin_path = NULL; - char buf[PATH_MAX]; if (path) { int len; @@ -7724,6 +7726,8 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) return -EINVAL; else if (len >= PATH_MAX) return -ENAMETOOLONG; + while ((s = strstr(s, "."))) + *s = '_'; pin_path = buf; } else if (!map->pin_path) { continue;