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: 1410265 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=BY7T6kbU; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CmrKL2Zf4z9s1l for ; Thu, 3 Dec 2020 20:35:02 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726003AbgLCJep (ORCPT ); Thu, 3 Dec 2020 04:34:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33937 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726070AbgLCJeo (ORCPT ); Thu, 3 Dec 2020 04:34:44 -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-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-311-DGOu9iIkNLCyN3IaBWzRgQ-1; Thu, 03 Dec 2020 04:33:17 -0500 X-MC-Unique: DGOu9iIkNLCyN3IaBWzRgQ-1 Received: by mail-ej1-f72.google.com with SMTP id a8so538302ejc.19 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=VRKumVx2T0Nl2IdCneut/xXeemD3zeJWh5lsOaM1rCloxgc5H+iTOYa1iP4ffouAil Bs9IRZ2+wjf/YsY1iHgBs1atKINxwsqYv59PQbbXGflpKo+rxwKmT+yOBXl6fGisddYl HB6L1mFr7AW/2AzimGwLwD3cWrcsoidPkttB7sFYaKd7gcswDimZthvh+dKhtxbrQ2vT WSH+SvG7D3SaTKjwH7+jQFaBcYo1qtkJIXzJx0Zp3myRSypmxGn5u7Z20Tj8zBl/IKRb Bzjg2u4EaKHAEdri2uht8OdVrhsj2HrYTpfIF1Pr9rW0plNivUxqgYid9U4ZAXSuwiAk /VhQ== X-Gm-Message-State: AOAM532L8AeH1kbxicsECDsir1OOYcu4+eGH98iLxtrVjKfhatV8kfIW tVQOZ/t4Zlb3VzT1Kwepc1T9I7HEmh/qvCOCnEKHCskohPkb/GPegNsFKLxATVwu/fEeaBo9MMU hSsP/DTKRXZp1kXXH X-Received: by 2002:a50:c091:: with SMTP id k17mr1974348edf.137.1606987995696; 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: 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 --- 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;