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: 335689 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=unavailable 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 46E63C64E7B 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 E4B302073C 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="YLBhpVdW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728738AbgK3QS7 (ORCPT ); Mon, 30 Nov 2020 11:18:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:52877 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727243AbgK3QS7 (ORCPT ); Mon, 30 Nov 2020 11:18:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606753052; 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=YLBhpVdWhioDYYd9f1bU5XdkE6cAr4zUOK4cXfHyti8mOjtVi+2dK+9lo665SLMzF/jQ3+ 3+2sBd1b7b7VxXlc8TZCef4k25V0j+4/OQ98zQjNa1hDYTEshBc3EpZs0xDIF8lXITPxFf 1H0Lb4479JBR28tWGlqB1cPvd7NU5Rk= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-184-IlCS2DrsM5Gdb-xUepJQBQ-1; Mon, 30 Nov 2020 11:17:28 -0500 X-MC-Unique: IlCS2DrsM5Gdb-xUepJQBQ-1 Received: by mail-qv1-f71.google.com with SMTP id o16so7847355qvq.4 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=lCx2iURf5lyRX0nVWKTh7CIByPhFcQKhgS9AqTBMvNPjifTr4gzx7I0r4CeIr2wdkP YwefwKen0Y+D0whOHZzYbr/zYARbMs0Asr8u4zdCvXbN7Scg0CCohkUA0KI6bwzdD5r8 qB9jrQ52SUqZxF+54YYv5gLyWHFPFPgn7p2Q4BzhpGpa7HKL7euzKJq9/4eCgnegxEVB pq1YH7nHcGTv74fevJ5a2YoBmT4yVTbQpimCB7lMyspUN5tl/MOVAIQbK+M1PPSgknrk TbQx1zVwvNUrQVhTSDI0Iu0pMm0HQKsxnJsH9Poc53slDbbZ4iXZBoeJnUzS4DhwJjtF XEpw== X-Gm-Message-State: AOAM533lsCAaus/lcKn2+xfPiw2mG3qFVa6Pj8lknlrPOsxLsSA8XGPp eQ+QOK03Wp4x0u5iWW1B5IUWvFnQysXMEbD/8BMVMLcr8xB7Xo9daOVvwWRtFEZGlwJBaj/aEaO CBdLrDM6ZBAtXLlmS X-Received: by 2002:a05:620a:15f7:: with SMTP id p23mr22454889qkm.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: 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 --- 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;