From patchwork Thu Dec 3 09:33:06 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: 11948285 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 6C32AC64E8A for ; Thu, 3 Dec 2020 09:34:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E06222241 for ; Thu, 3 Dec 2020 09:34:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728037AbgLCJep (ORCPT ); Thu, 3 Dec 2020 04:34:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47932 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726003AbgLCJep (ORCPT ); Thu, 3 Dec 2020 04:34:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606987998; 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=uBzMWsgGtbXcdYHHTMh1lOFsxe85/SSnatB0cA3Z1Ew=; b=BY7T6kbUyPiYJBsTRI7edAJ/mtaBWIrFITBr200MZpb6IKt/0vDmgIF88XllzEDMD/LB1V mhFc4hbt5w1oyJlu2dx0aMoyHsVZxr61qC/I7qDTCw2c8Wz6EX537rjrtMv6WGDZgUlMeL WDKgf1fvPNePImIYZ+3eSyKJD4riSVg= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-311-t-KKdQ4ZN7uMJ2z1WV-svA-1; Thu, 03 Dec 2020 04:33:16 -0500 X-MC-Unique: t-KKdQ4ZN7uMJ2z1WV-svA-1 Received: by mail-ed1-f69.google.com with SMTP id i15so709135edx.9 for ; Thu, 03 Dec 2020 01:33:16 -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=uBzMWsgGtbXcdYHHTMh1lOFsxe85/SSnatB0cA3Z1Ew=; b=atyKqiq04YLm3Y8X8mW9JzMTrCRvECWvtkLDqCr+wtTZxBFpsVT79wa0aN5bL4PlBx iUKs8o3poN2NkNvL2DzNil0X38/xETJqYYsGrzMO72s6Ps3HtQV+2HvefM5wGOEqOzqQ 3hiQEZkYlBKmrSq9w5KoAhShUlV0J9AKYV7jRs96PVyp/dWk3aYt0KAJI6yrkdZpyQ9q ZMU5eZK/y5T1E20x5g/jizjF7veAfLTzsH2s8nL5LMxGm0P/tLlxBx8UzpQ0XMSFED04 AdgqCj1yP/IB9qvM8a365HSJ7Y5kfWwBv13mPUL0cSSjxSSaKV70561aIotNfh6pv0Fg GRLw== X-Gm-Message-State: AOAM530Q3o993Wm+aBm4IRO8xHfL9GXy9FaAWYodf6W2Tgr95EW3kHnz IJ23cGIMnrRfYaBS7phl2pusRKQ4bXOIBE6mF4uXsR4KKvq1+fTfZgfxZhBopF4ldRzKlvsvQWH 1+LyPwcLI6iJn X-Received: by 2002:a50:c091:: with SMTP id k17mr1974351edf.137.1606987995698; Thu, 03 Dec 2020 01:33:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxpcjscdDLv61o6nvrf4IeUgMNWts9rglY+4xclEtDM+Dqefb97EEaPbuarL1HUhxKKoEp1YQ== X-Received: by 2002:a50:c091:: with SMTP id k17mr1974343edf.137.1606987995554; Thu, 03 Dec 2020 01:33:15 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id gt11sm423487ejb.67.2020.12.03.01.33.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2020 01:33:15 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 83630181CF8; Thu, 3 Dec 2020 10:33:14 +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 v3] libbpf: sanitise map names before pinning Date: Thu, 3 Dec 2020 10:33:06 +0100 Message-Id: <20201203093306.107676-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 --- v3: - void return for helper function v2: - Move string munging to helper function tools/lib/bpf/libbpf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8d05132e1945..4a933ae64297 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7651,6 +7651,16 @@ bool bpf_map__is_pinned(const struct bpf_map *map) return map->pinned; } +static void sanitize_pin_path(char *s) +{ + /* bpffs disallows periods in path names */ + while (*s) { + if (*s == '.') + *s = '_'; + s++; + } +} + int bpf_object__pin_maps(struct bpf_object *obj, const char *path) { struct bpf_map *map; @@ -7680,6 +7690,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path) err = -ENAMETOOLONG; goto err_unpin_maps; } + sanitize_pin_path(buf); pin_path = buf; } else if (!map->pin_path) { continue; @@ -7724,6 +7735,7 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) return -EINVAL; else if (len >= PATH_MAX) return -ENAMETOOLONG; + sanitize_pin_path(buf); pin_path = buf; } else if (!map->pin_path) { continue;