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: 11945707 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 1497EC64E7C for ; Wed, 2 Dec 2020 11:20:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ACEC5206E0 for ; Wed, 2 Dec 2020 11:20:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726055AbgLBLU0 (ORCPT ); Wed, 2 Dec 2020 06:20:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:44067 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725995AbgLBLUZ (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=1606907938; 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=XzBkhWLMPFuILrCZc4Yn/QvZY6Efcu7D9Ow6zZlR9A1GpHcjY+of6gbo/BxI9rW6OfIk8C 6J5r+EgcMPam5AqEY+1qz+nnk2D1GC+m/3KGGztv+Ry5GlS3Z/Ss0uUnBE/gRn3T7FR9zG F1f2jMtmwmWLG0nJEM1+zWTob5zDscM= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-217-YonyaH51MI-918w9gNXThw-1; Wed, 02 Dec 2020 06:18:57 -0500 X-MC-Unique: YonyaH51MI-918w9gNXThw-1 Received: by mail-qv1-f69.google.com with SMTP id m45so946342qvg.16 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=kAxHrJ6a2LVtR/dA3Q12cHpU22xUTAF67TpeUsF9drvU7Db+EnDeafg2BW7yGXeDRu 8ftYXsj2RWv3tFkABgFXzpIN8bN6cV4mgeaDCiSqIo0U1pesJQIidaHF88/JyqGwrC07 rQlhK8zG0bEhDQ808fglCLAwmYVKTgqeqNHqwDZZAfR6UGRFVvD2udD0LYdP9ge7qW8U afz3jG5h0ctDkaDDfMWVdFr9thchq4ZfLDDXOBbav+07Taibd3AMNR4MACiIP/JSv6P2 ypxCWhSKQDSrqc+ro1KbLF10u9qGlr8osr0Xb1FM6qS8e9Z84qca8csvCJXK62+3UVsN KpfA== X-Gm-Message-State: AOAM533wcg4Q8weFXs78Gsk30phEm2V9UqXRjUO+42XUH6gXZGsajDbq gOeqixL12GaIvyptH3+5AtM/C8xFtyukI+W0lWIe5TNRmt+Rn/O4iiBvl0THAPcexlLf8ENVWdE FTq128cxwGeRf X-Received: by 2002:aed:38c8:: with SMTP id k66mr1988931qte.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: 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 --- 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; }