From patchwork Sat Aug 21 18:48:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 12450987 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=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, 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 35400C4338F for ; Sat, 21 Aug 2021 18:48:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1A9BF611ED for ; Sat, 21 Aug 2021 18:48:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231136AbhHUStP (ORCPT ); Sat, 21 Aug 2021 14:49:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230486AbhHUStM (ORCPT ); Sat, 21 Aug 2021 14:49:12 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77881C061575; Sat, 21 Aug 2021 11:48:31 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id oa17so9404776pjb.1; Sat, 21 Aug 2021 11:48:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MImGISiGbEdyd0KJKmcKEPAlE/kvVCnnBDWV84DHMTw=; b=giMxW2IHIA0Ges++2Lbm/56yEv6T9rZyXXgR/6A+D3q8h/ENC6LI8hjpw4rJtrJqCN mB9KmW6qFHivXf5YaWv6RnX6v6eYNLjm2/YWzLAYGRRU11wSOyGq9CJy50zRWj71WYDO rE56wJFBubG4GtwOK/PywtgjuJgCNvjgtCvumUaCAOAC+wr/fubn3fBdSsYb3YxZMR35 mNO33KEqFL9oEiy2xrCKq0Z9bvI+/hYd46UTdT11EbX0czExAL5YrbsNb7s+p+3zV9Yo dgIvTwkcrc/3WkmZ2VjO67CG8qA/Fx3LpYej/zAtxdaczN7x+e1JrNAoN+36fLzZsR3I F3BQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=MImGISiGbEdyd0KJKmcKEPAlE/kvVCnnBDWV84DHMTw=; b=pxxGCB/A8fqBqgaN1XKAyJ7dnTk/w1YP6eh9eSc1J0Bb2lMxBam7Zz8bKhsq/a5lsg aSNq4amuap3vleALdT00cvtrzEa2RQJrT94keBea/oDR4+tk3Tk/d71Wv/MREMH41ZRw xKYlJc6FB9uVAN42l3q0nMA3rVJKfGaWqgLCrJjK5TYeIn2mxGAJcg96vDBS3k6zQlYq NuaFGM2P1dp5kRcVrgyNR9qUfQN52G4CqSuh909tCP79w28UJX/zFZ3DC+WCYkd8gNTo 0exo44cBJ9knXj8JUZZTFIz9TFBdo/OGWAAJLHc92yQ1M6qj1ijWHRw3EPL6UODzsL+9 urEw== X-Gm-Message-State: AOAM5312/m+NT+RZxQH8qSrVCVjKSCRU2TsG2Fyw1vMUd8H4T9Xi/oX4 Myn1QLzMRf44to1JrElDtzw5jGwPuNA= X-Google-Smtp-Source: ABdhPJyd33S35yGn3QYnP5WQkJPYk1fRRGPvy122/Fsr7nj6xmIm+fIOAlrBhnYqdLZMBbPKLP7Cdw== X-Received: by 2002:a17:90a:ab07:: with SMTP id m7mr10888803pjq.27.1629571710857; Sat, 21 Aug 2021 11:48:30 -0700 (PDT) Received: from localhost ([2405:201:6014:d820:9cc6:d37f:c2fd:dc6]) by smtp.gmail.com with ESMTPSA id b15sm12784182pgj.60.2021.08.21.11.48.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 11:48:30 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , KP Singh , Spencer Baugh , Andy Lutomirski , Pavel Emelyanov , Alexander Mihalicyn , Andrei Vagin , linux-security-module@vger.kernel.org Subject: [PATCH bpf-next RFC v1 1/5] bpf: Implement file local storage Date: Sun, 22 Aug 2021 00:18:20 +0530 Message-Id: <20210821184824.2052643-2-memxor@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821184824.2052643-1-memxor@gmail.com> References: <20210821184824.2052643-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: This map is useful in general to tie data associated with a open file (not fd) from eBPF programs, such that data is released when the file goes away (e.g. checkpoint/restore usecase). Another usecase is implementing Capsicum [0] style capability sandbox in userspace using eBPF LSM, enforcing rights at the file level using this mechanism. The implementation is exactly similar to bpf_inode_storage, with some minor changes where required. [0]: https://www.usenix.org/legacy/event/sec10/tech/full_papers/Watson.pdf Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_lsm.h | 21 +++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 39 ++++++ kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_file_storage.c | 244 ++++++++++++++++++++++++++++++++++ kernel/bpf/bpf_lsm.c | 4 + kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 ++ security/bpf/hooks.c | 2 + 9 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 kernel/bpf/bpf_file_storage.c diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h index 479c101546ad..5901a39cd5ac 100644 --- a/include/linux/bpf_lsm.h +++ b/include/linux/bpf_lsm.h @@ -42,6 +42,18 @@ extern const struct bpf_func_proto bpf_inode_storage_get_proto; extern const struct bpf_func_proto bpf_inode_storage_delete_proto; void bpf_inode_storage_free(struct inode *inode); +static inline struct bpf_storage_blob *bpf_file(const struct file *file) +{ + if (unlikely(!file->f_security)) + return NULL; + + return file->f_security + bpf_lsm_blob_sizes.lbs_file; +} + +extern const struct bpf_func_proto bpf_file_storage_get_proto; +extern const struct bpf_func_proto bpf_file_storage_delete_proto; +void bpf_file_storage_free(struct file *file); + #else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook(u32 btf_id) @@ -65,6 +77,15 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline struct bpf_storage_blob *bpf_file(const struct file *file) +{ + return NULL; +} + +static inline void bpf_file_storage_free(struct file *file) +{ +} + #endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */ diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h index 9c81724e4b98..c68cc6d9e7da 100644 --- a/include/linux/bpf_types.h +++ b/include/linux/bpf_types.h @@ -107,6 +107,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops) #ifdef CONFIG_BPF_LSM BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_FILE_STORAGE, file_storage_map_ops) #endif BPF_MAP_TYPE(BPF_MAP_TYPE_TASK_STORAGE, task_storage_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c4f7892edb2b..d4bf4e4d56b5 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -906,6 +906,7 @@ enum bpf_map_type { BPF_MAP_TYPE_RINGBUF, BPF_MAP_TYPE_INODE_STORAGE, BPF_MAP_TYPE_TASK_STORAGE, + BPF_MAP_TYPE_FILE_STORAGE, }; /* Note that tracing related programs such as @@ -4871,6 +4872,42 @@ union bpf_attr { * Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. + * + * void *bpf_file_storage_get(struct bpf_map *map, void *file, void *value, u64 flags) + * Description + * Get a bpf_local_storage from a *file*. + * + * Logically, it could be thought of as getting the value from + * a *map* with *file* as the **key**. From this + * perspective, the usage is not much different from + * **bpf_map_lookup_elem**\ (*map*, **&**\ *file*) except this + * helper enforces the key must be an file and the map must also + * be a **BPF_MAP_TYPE_FILE_STORAGE**. + * + * Underneath, the value is stored locally at *file* instead of + * the *map*. The *map* is used as the bpf-local-storage + * "type". The bpf-local-storage "type" (i.e. the *map*) is + * searched against all bpf_local_storage residing at *file*. + * + * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be + * used such that a new bpf_local_storage will be + * created if one does not exist. *value* can be used + * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify + * the initial value of a bpf_local_storage. If *value* is + * **NULL**, the new bpf_local_storage will be zero initialized. + * Return + * A bpf_local_storage pointer is returned on success. + * + * **NULL** if not found or there was an error in adding + * a new bpf_local_storage. + * + * int bpf_file_storage_delete(struct bpf_map *map, void *file) + * Description + * Delete a bpf_local_storage from a *file*. + * Return + * 0 on success. + * + * **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5048,6 +5085,8 @@ union bpf_attr { FN(timer_cancel), \ FN(get_func_ip), \ FN(get_attach_cookie), \ + FN(file_storage_get), \ + FN(file_storage_delete), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 7f33098ca63f..98a18e402a0a 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -10,7 +10,7 @@ obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_i obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o -obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o +obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o bpf_file_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o diff --git a/kernel/bpf/bpf_file_storage.c b/kernel/bpf/bpf_file_storage.c new file mode 100644 index 000000000000..c826bc0405c4 --- /dev/null +++ b/kernel/bpf/bpf_file_storage.c @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +DEFINE_BPF_STORAGE_CACHE(file_cache); + +static struct bpf_local_storage __rcu **file_storage_ptr(void *owner) +{ + struct bpf_storage_blob *bsb; + struct file *file = owner; + + bsb = bpf_file(file); + if (!bsb) + return NULL; + return &bsb->storage; +} + +static struct bpf_local_storage_data * +file_storage_lookup(struct file *file, struct bpf_map *map, bool cacheit_lockit) +{ + struct bpf_local_storage *file_storage; + struct bpf_local_storage_map *smap; + struct bpf_storage_blob *bsb; + + bsb = bpf_file(file); + if (!bsb) + return NULL; + + file_storage = rcu_dereference(bsb->storage); + if (!file_storage) + return NULL; + + smap = (struct bpf_local_storage_map *)map; + return bpf_local_storage_lookup(file_storage, smap, cacheit_lockit); +} + +void bpf_file_storage_free(struct file *file) +{ + struct bpf_local_storage *local_storage; + struct bpf_local_storage_elem *selem; + bool free_file_storage = false; + struct bpf_storage_blob *bsb; + struct hlist_node *n; + + bsb = bpf_file(file); + if (!bsb) + return; + + rcu_read_lock(); + + local_storage = rcu_dereference(bsb->storage); + if (!local_storage) { + rcu_read_unlock(); + return; + } + + raw_spin_lock_bh(&local_storage->lock); + hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) { + bpf_selem_unlink_map(selem); + free_file_storage = bpf_selem_unlink_storage_nolock(local_storage, + selem, false); + } + raw_spin_unlock_bh(&local_storage->lock); + rcu_read_unlock(); + + if (free_file_storage) + kfree_rcu(local_storage, rcu); +} + +static void *bpf_fd_file_storage_lookup_elem(struct bpf_map *map, void *key) +{ + struct bpf_local_storage_data *sdata; + struct file *file; + int fd; + + fd = *(int *)key; + file = fget_raw(fd); + if (!file) + return ERR_PTR(-EBADF); + + sdata = file_storage_lookup(file, map, true); + fput(file); + return sdata ? sdata->data : NULL; +} + +static int bpf_fd_file_storage_update_elem(struct bpf_map *map, void *key, + void *value, u64 map_flags) +{ + struct bpf_local_storage_data *sdata; + struct file *file; + int fd; + + fd = *(int *)key; + file = fget_raw(fd); + if (!file) + return -EBADF; + if (!file_storage_ptr(file)) { + fput(file); + return -EBADF; + } + + sdata = bpf_local_storage_update(file, + (struct bpf_local_storage_map *)map, + value, map_flags); + fput(file); + return PTR_ERR_OR_ZERO(sdata); +} + +static int file_storage_delete(struct file *file, struct bpf_map *map) +{ + struct bpf_local_storage_data *sdata; + + sdata = file_storage_lookup(file, map, false); + if (!sdata) + return -ENOENT; + + bpf_selem_unlink(SELEM(sdata)); + + return 0; +} + +static int bpf_fd_file_storage_delete_elem(struct bpf_map *map, void *key) +{ + struct file *file; + int fd, err; + + fd = *(int *)key; + file = fget_raw(fd); + if (!file) + return -EBADF; + + err = file_storage_delete(file, map); + fput(file); + return err; +} + +BPF_CALL_4(bpf_file_storage_get, struct bpf_map *, map, struct file *, file, + void *, value, u64, flags) +{ + struct bpf_local_storage_data *sdata; + + if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) + return (unsigned long)NULL; + + if (!file || !file_storage_ptr(file)) + return (unsigned long)NULL; + + sdata = file_storage_lookup(file, map, true); + if (sdata) + return (unsigned long)sdata->data; + + if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) { + sdata = bpf_local_storage_update( + file, (struct bpf_local_storage_map *)map, value, + BPF_NOEXIST); + return IS_ERR(sdata) ? (unsigned long)NULL : + (unsigned long)sdata->data; + } + + return (unsigned long)NULL; +} + +BPF_CALL_2(bpf_file_storage_delete, struct bpf_map *, map, struct file *, file) +{ + if (!file) + return -EINVAL; + + return file_storage_delete(file, map); +} + +static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key) +{ + return -ENOTSUPP; +} + +static struct bpf_map *file_storage_map_alloc(union bpf_attr *attr) +{ + struct bpf_local_storage_map *smap; + + smap = bpf_local_storage_map_alloc(attr); + if (IS_ERR(smap)) + return ERR_CAST(smap); + + smap->cache_idx = bpf_local_storage_cache_idx_get(&file_cache); + return &smap->map; +} + +static void file_storage_map_free(struct bpf_map *map) +{ + struct bpf_local_storage_map *smap; + + smap = (struct bpf_local_storage_map *)map; + bpf_local_storage_cache_idx_free(&file_cache, smap->cache_idx); + bpf_local_storage_map_free(smap, NULL); +} + +static int file_storage_map_btf_id; + +const struct bpf_map_ops file_storage_map_ops = { + .map_meta_equal = bpf_map_meta_equal, + .map_alloc_check = bpf_local_storage_map_alloc_check, + .map_alloc = file_storage_map_alloc, + .map_free = file_storage_map_free, + .map_get_next_key = notsupp_get_next_key, + .map_lookup_elem = bpf_fd_file_storage_lookup_elem, + .map_update_elem = bpf_fd_file_storage_update_elem, + .map_delete_elem = bpf_fd_file_storage_delete_elem, + .map_check_btf = bpf_local_storage_map_check_btf, + .map_btf_name = "bpf_local_storage_map", + .map_btf_id = &file_storage_map_btf_id, + .map_owner_storage_ptr = file_storage_ptr, +}; + +BTF_ID_LIST_SINGLE(bpf_file_storage_btf_ids, struct, file) + +const struct bpf_func_proto bpf_file_storage_get_proto = { + .func = bpf_file_storage_get, + .gpl_only = false, + .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_PTR_TO_BTF_ID, + .arg2_btf_id = &bpf_file_storage_btf_ids[0], + .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, + .arg4_type = ARG_ANYTHING, +}; + +const struct bpf_func_proto bpf_file_storage_delete_proto = { + .func = bpf_file_storage_delete, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_PTR_TO_BTF_ID, + .arg2_btf_id = &bpf_file_storage_btf_ids[0], +}; diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 06062370c3b8..48c2022fd958 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -121,6 +121,10 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_bprm_opts_set_proto; case BPF_FUNC_ima_inode_hash: return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL; + case BPF_FUNC_file_storage_get: + return &bpf_file_storage_get_proto; + case BPF_FUNC_file_storage_delete: + return &bpf_file_storage_delete_proto; default: return tracing_prog_func_proto(func_id, prog); } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4e50c0bfdb7d..946a85945776 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -783,7 +783,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && map->map_type != BPF_MAP_TYPE_SK_STORAGE && map->map_type != BPF_MAP_TYPE_INODE_STORAGE && - map->map_type != BPF_MAP_TYPE_TASK_STORAGE) + map->map_type != BPF_MAP_TYPE_TASK_STORAGE && + map->map_type != BPF_MAP_TYPE_FILE_STORAGE) return -ENOTSUPP; if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > map->value_size) { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f5a0077c9981..dc615658c92d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5390,6 +5390,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_task_storage_delete) goto error; break; + case BPF_MAP_TYPE_FILE_STORAGE: + if (func_id != BPF_FUNC_file_storage_get && + func_id != BPF_FUNC_file_storage_delete) + goto error; + break; default: break; } @@ -5473,6 +5478,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) goto error; break; + case BPF_FUNC_file_storage_get: + case BPF_FUNC_file_storage_delete: + if (map->map_type != BPF_MAP_TYPE_FILE_STORAGE) + goto error; + break; default: break; } diff --git a/security/bpf/hooks.c b/security/bpf/hooks.c index e5971fa74fd7..faa70467db4d 100644 --- a/security/bpf/hooks.c +++ b/security/bpf/hooks.c @@ -13,6 +13,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { #undef LSM_HOOK LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), LSM_HOOK_INIT(task_free, bpf_task_storage_free), + LSM_HOOK_INIT(file_free_security, bpf_file_storage_free), }; static int __init bpf_lsm_init(void) @@ -25,6 +26,7 @@ static int __init bpf_lsm_init(void) struct lsm_blob_sizes bpf_lsm_blob_sizes __lsm_ro_after_init = { .lbs_inode = sizeof(struct bpf_storage_blob), .lbs_task = sizeof(struct bpf_storage_blob), + .lbs_file = sizeof(struct bpf_storage_blob), }; DEFINE_LSM(bpf) = { From patchwork Sat Aug 21 18:48:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 12450989 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 84244C4320E for ; Sat, 21 Aug 2021 18:48:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5893C61262 for ; Sat, 21 Aug 2021 18:48:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230486AbhHUStQ (ORCPT ); Sat, 21 Aug 2021 14:49:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231159AbhHUStN (ORCPT ); Sat, 21 Aug 2021 14:49:13 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35A37C061575; Sat, 21 Aug 2021 11:48:34 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id b9so3434942plx.2; Sat, 21 Aug 2021 11:48:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7aQ8+MWaUwrYV+7bA2BdzDeH1tSNPbIkq3oxgaie3l0=; b=qkIG6uOnhfGhm4h2ah9Mfn8r9Ts4Iu9fxNJm42Jda8qsbueVVzxmFZ8KSkUitrVrP9 92cHv72NkgOLp/H9BM9y+LjMV1qTo3noz3yWgw9uODjLMGXknB5kk7itMB4m2xEMbruM t9oC0yahHZMBIN+C/Y4VesTHvzdblYCw7PIbaBw0FP/JUekRfd0zCUQwXzTaKYWsNl2j uTsvj8Hq5sUFHf/TKpNfUYe8KN7qkYGtTApPEHE4NHmIcp6bL6DpqO7+aAgOivo2lzxm nLDbj2+vb6rJxulu2nv8xOOVmSMaHsHcr9CHP0jjaQc554Ctd5m1ouyaGiKH/aIVMdhm 1zLw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=7aQ8+MWaUwrYV+7bA2BdzDeH1tSNPbIkq3oxgaie3l0=; b=msDnrZyPA92MN68MVG7EtpfD4j/SuCBp8MMqDklw3mbsaOqyz48FKFwyOtlEbcrvgD g7gH47ur1sLIaPMW0yWFx8v3jE3dIYWWdW+vaq/e50VfymJHqGzYV44bJLmOt13rN4c5 ikqVBj6FM5Wj4KAFS8ksM9jaNeemeQ9xYZ7wXKom5sSJRpVDqBODRXkudNVJGVr+EZhL B+eciEx5h6Dg5E+sZrK3zkUXALRKm9GNoS+aa2+27xPhfRsp0PDv7DJaKaS0NcBfMwPD YkDY+M5C+bukElpJY78++ndVtRF9vT3GbRpgkSC0neoYijQWG+Q7fF+NWcOabuOlhG2G cqHw== X-Gm-Message-State: AOAM531FvnXnn3sTa4viGiBbbuontvF7NcHBnENHnJUH7BbsIzOsqUIP qIRXBmiHV1egZSBSoqeRFrsyo5N0Bfw= X-Google-Smtp-Source: ABdhPJyKW5oBpTr362SlqTzjB1Z++hwx0rb3NvujMC1hqtKYjVrrE0+i0eC6eLqXVYHaz/nunYOXnA== X-Received: by 2002:a17:90a:a581:: with SMTP id b1mr10792277pjq.153.1629571713626; Sat, 21 Aug 2021 11:48:33 -0700 (PDT) Received: from localhost ([2405:201:6014:d820:9cc6:d37f:c2fd:dc6]) by smtp.gmail.com with ESMTPSA id m2sm11073994pfo.45.2021.08.21.11.48.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 11:48:33 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , KP Singh , Spencer Baugh , Andy Lutomirski , Pavel Emelyanov , Alexander Mihalicyn , Andrei Vagin , linux-security-module@vger.kernel.org Subject: [PATCH bpf-next RFC v1 2/5] tools: sync bpf.h header Date: Sun, 22 Aug 2021 00:18:21 +0530 Message-Id: <20210821184824.2052643-3-memxor@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821184824.2052643-1-memxor@gmail.com> References: <20210821184824.2052643-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Update the bpf.h UAPI header with changes made from file local storage additions. Signed-off-by: Kumar Kartikeya Dwivedi --- tools/include/uapi/linux/bpf.h | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c4f7892edb2b..d4bf4e4d56b5 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -906,6 +906,7 @@ enum bpf_map_type { BPF_MAP_TYPE_RINGBUF, BPF_MAP_TYPE_INODE_STORAGE, BPF_MAP_TYPE_TASK_STORAGE, + BPF_MAP_TYPE_FILE_STORAGE, }; /* Note that tracing related programs such as @@ -4871,6 +4872,42 @@ union bpf_attr { * Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. + * + * void *bpf_file_storage_get(struct bpf_map *map, void *file, void *value, u64 flags) + * Description + * Get a bpf_local_storage from a *file*. + * + * Logically, it could be thought of as getting the value from + * a *map* with *file* as the **key**. From this + * perspective, the usage is not much different from + * **bpf_map_lookup_elem**\ (*map*, **&**\ *file*) except this + * helper enforces the key must be an file and the map must also + * be a **BPF_MAP_TYPE_FILE_STORAGE**. + * + * Underneath, the value is stored locally at *file* instead of + * the *map*. The *map* is used as the bpf-local-storage + * "type". The bpf-local-storage "type" (i.e. the *map*) is + * searched against all bpf_local_storage residing at *file*. + * + * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be + * used such that a new bpf_local_storage will be + * created if one does not exist. *value* can be used + * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify + * the initial value of a bpf_local_storage. If *value* is + * **NULL**, the new bpf_local_storage will be zero initialized. + * Return + * A bpf_local_storage pointer is returned on success. + * + * **NULL** if not found or there was an error in adding + * a new bpf_local_storage. + * + * int bpf_file_storage_delete(struct bpf_map *map, void *file) + * Description + * Delete a bpf_local_storage from a *file*. + * Return + * 0 on success. + * + * **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5048,6 +5085,8 @@ union bpf_attr { FN(timer_cancel), \ FN(get_func_ip), \ FN(get_attach_cookie), \ + FN(file_storage_get), \ + FN(file_storage_delete), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Sat Aug 21 18:48:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 12450991 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 D28F4C43214 for ; Sat, 21 Aug 2021 18:48:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BDBB5611ED for ; Sat, 21 Aug 2021 18:48:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231130AbhHUStR (ORCPT ); Sat, 21 Aug 2021 14:49:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbhHUStQ (ORCPT ); Sat, 21 Aug 2021 14:49:16 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAE67C061575; Sat, 21 Aug 2021 11:48:36 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id r2so12530189pgl.10; Sat, 21 Aug 2021 11:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SQ8X68+agmo/9YIuGhT9zb0QxYHN2zLKWc/jICvz7q0=; b=lE0IRB8OG2q5j/tYcPIEDzgS3giaqAJfQKgv3P2bSqhtqmtMtd8ZQItdWp3WnvroQg rcgPpHBq+IA+V+T4Viq0yuHd759GJPMFDgb4cxMyJA/2cPjTVLWZqiVWxHfuy0/S4PoG 8a5BxScww1mucJ5mC5wYNSFAYFpn2RaLNioHe5MVO20y6DhSN41Ibc2Ztjgk9RcKHiFG vMc8hATZzT0MandcXWf7D0Lo4CtesJwtXdy/8jVpPA2RmOZ2y9ZXzkqQgeDSE9cegob1 1CT1/hp/g1053Y5KEAAPmKKZbnUblDE0tRHg/ixA3sOynglvufYSNi06wqioBOT/hGCN wC0w== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=SQ8X68+agmo/9YIuGhT9zb0QxYHN2zLKWc/jICvz7q0=; b=SoG+Ofja5J6jm9Fxp3aBsgQnYJXgEQX8PKFouBq/WOM56fG9JDHlvl8qBgUEhejQ9j hCI2Ec2Hq7DiJ+zujrdIk3XsDaPseveufLM8Ulw3PTWbLXmoQ3uGijf/sMAMywSxm/Jy BmimNwuRhOMzkx5aMT0sTTmmeoyDr27wquYYmBJqNlQPi97NC/IH6Tr6taJMqep471F0 F28lhY2h5UOsyLCHv4LoUuLdq+y2IUOccFBVp68TqPfYf0MiQ0GaAZwHRHMSQbjw6nPc eQZuRPKSIbawhoc57hx6smqgeHBnzUPjOGjHkeg+TWNH28F5c9magfSPqV2tgTBc52gQ sdIA== X-Gm-Message-State: AOAM5329bsvksNPcG9zBlr4KYJqkv3992Gc91Zy6pfNAEaGgACMx+wCx bDzf9BpuVZmqYOqGcC+DcX9zUjpPbig= X-Google-Smtp-Source: ABdhPJyD7hQIC2+w9JKiP3Uk6xHcsU+fZOTkz8/NmcQHPP2gYr09/AUYSsDl/EoaBmZLbCkmvN0qLg== X-Received: by 2002:a63:6c5:: with SMTP id 188mr24870073pgg.39.1629571716387; Sat, 21 Aug 2021 11:48:36 -0700 (PDT) Received: from localhost ([2405:201:6014:d820:9cc6:d37f:c2fd:dc6]) by smtp.gmail.com with ESMTPSA id pc11sm14729093pjb.17.2021.08.21.11.48.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 11:48:36 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , KP Singh , Spencer Baugh , Andy Lutomirski , Pavel Emelyanov , Alexander Mihalicyn , Andrei Vagin , linux-security-module@vger.kernel.org Subject: [PATCH bpf-next RFC v1 3/5] libbpf: Add bpf_probe_map_type support for file local storage Date: Sun, 22 Aug 2021 00:18:22 +0530 Message-Id: <20210821184824.2052643-4-memxor@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821184824.2052643-1-memxor@gmail.com> References: <20210821184824.2052643-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Signed-off-by: Kumar Kartikeya Dwivedi --- tools/lib/bpf/libbpf_probes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index cd8c703dde71..a97f2088c53a 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -233,6 +233,7 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex) case BPF_MAP_TYPE_SK_STORAGE: case BPF_MAP_TYPE_INODE_STORAGE: case BPF_MAP_TYPE_TASK_STORAGE: + case BPF_MAP_TYPE_FILE_STORAGE: btf_key_type_id = 1; btf_value_type_id = 3; value_size = 8; From patchwork Sat Aug 21 18:48:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 12450993 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 9202AC4338F for ; Sat, 21 Aug 2021 18:48:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B21661165 for ; Sat, 21 Aug 2021 18:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231181AbhHUStU (ORCPT ); Sat, 21 Aug 2021 14:49:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbhHUStT (ORCPT ); Sat, 21 Aug 2021 14:49:19 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27E2AC061575; Sat, 21 Aug 2021 11:48:40 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id fa24-20020a17090af0d8b0290178bfa69d97so9497951pjb.0; Sat, 21 Aug 2021 11:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VZddMfhMy2qsp873EeyfgTUq4LH9HW+30X5gjNhM9wo=; b=bmquxEjHBtdGMSRvccpdM7/fghIGiLxLV/zF0d1abeel6FJWYJmOkpjBbRprZAMlRg ZH5kb7JXG2GxaMtfFVVIX2oyQQwzzxmtILosvre+YLmzScTu7Kk2hxQuoGDA3r+7z16l 39ZbAnclqpcoyi7yaYtEU+Cc386gwkWtJgDA++z80B4hG5vVkg5mdgnyq0xwY7Un1fv3 AWXDlkIX7xvb8xAJrEQ/4Rrgnz8WXicev8MjwV0z3ogRfQDKYplTV+Gq2IeYJrGCTYBO RRvkfc8+su0l/f/+thjy/HUdnEL+pzkt5c2sKoietiYbFWZ+yBjRX6KFhLKm4AWfRf8P zMvA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=VZddMfhMy2qsp873EeyfgTUq4LH9HW+30X5gjNhM9wo=; b=bCD5twVHF4zziwCz3WqvlfhmRprVM1RB//cRsLCFuHlQ4uKW/dOLCEDecFlYXpS7tG aqU7iJNMaGU5a7oT6dmBlFBTR+z014O5Cd72dNuXSH//6/q1j3Wxxlg6FrdTRWru3eDc KlkM2Io3J1oUB5Hkp5Zk9ZhCXGRRRioW7Q17Prm89WUYOUZxE6fk66/T+svvQKSgmsbU IxAWHsrUjbUQkkMdqoIzppZr13LNCcYvi5t253x/pwPU7/creujcJhCNyIhcw2s7BXrB lK1RaHb2xE5HpsCIPQJ6Yo81HSyiw4X4fZrOqvlThbbCyCa37T4UJqC3f/1Bi2DfKvmt 2pig== X-Gm-Message-State: AOAM531tshS9y2k2bq3szRmXOM2zcr/be4lMsCIanE6HzZ8Hut9Pjxuw l3ftFuhfpprId7ICBM8XIyWxfEHI7IQ= X-Google-Smtp-Source: ABdhPJyp3LyoghFddr8lX9qvWQo5pKIQJ5wKj3tssSUcAMvKg7bpbywQTkte+teU52IbeXRuT7+Ltg== X-Received: by 2002:a17:902:9346:b0:132:6ba5:109 with SMTP id g6-20020a170902934600b001326ba50109mr3168144plp.19.1629571719546; Sat, 21 Aug 2021 11:48:39 -0700 (PDT) Received: from localhost ([2405:201:6014:d820:9cc6:d37f:c2fd:dc6]) by smtp.gmail.com with ESMTPSA id 65sm12520856pgi.12.2021.08.21.11.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 11:48:39 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , KP Singh , Spencer Baugh , Andy Lutomirski , Pavel Emelyanov , Alexander Mihalicyn , Andrei Vagin , linux-security-module@vger.kernel.org Subject: [PATCH bpf-next RFC v1 4/5] tools: bpf: update bpftool for file_storage map Date: Sun, 22 Aug 2021 00:18:23 +0530 Message-Id: <20210821184824.2052643-5-memxor@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821184824.2052643-1-memxor@gmail.com> References: <20210821184824.2052643-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: This updates bpftool to recognise the new file local storage map type. Signed-off-by: Kumar Kartikeya Dwivedi --- tools/bpf/bpftool/Documentation/bpftool-map.rst | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 3 ++- tools/bpf/bpftool/map.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-map.rst b/tools/bpf/bpftool/Documentation/bpftool-map.rst index d0c4abe08aba..aff192eb6e37 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-map.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-map.rst @@ -52,7 +52,7 @@ MAP COMMANDS | | **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash** | | **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage** | | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage** - | **task_storage** } + | **task_storage** | **file_storage** } DESCRIPTION =========== diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 88e2bcf16cca..e7939e82bda4 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -710,7 +710,8 @@ _bpftool() hash_of_maps devmap devmap_hash sockmap cpumap \ xskmap sockhash cgroup_storage reuseport_sockarray \ percpu_cgroup_storage queue stack sk_storage \ - struct_ops inode_storage task_storage ringbuf' + struct_ops inode_storage task_storage ringbuf \ + file_storage' COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) ) return 0 ;; diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 407071d54ab1..f3c6ea47f846 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -52,6 +52,7 @@ const char * const map_type_name[] = { [BPF_MAP_TYPE_RINGBUF] = "ringbuf", [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", + [BPF_MAP_TYPE_FILE_STORAGE] = "file_storage", }; const size_t map_type_name_size = ARRAY_SIZE(map_type_name); @@ -1466,7 +1467,7 @@ static int do_help(int argc, char **argv) " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n" " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n" " queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n" - " task_storage }\n" + " task_storage | file_storage }\n" " " HELP_SPEC_OPTIONS " |\n" " {-f|--bpffs} | {-n|--nomount} }\n" "", From patchwork Sat Aug 21 18:48:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 12450995 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=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, 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 826F0C4338F for ; Sat, 21 Aug 2021 18:48:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 64E8461165 for ; Sat, 21 Aug 2021 18:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231374AbhHUStY (ORCPT ); Sat, 21 Aug 2021 14:49:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231674AbhHUStX (ORCPT ); Sat, 21 Aug 2021 14:49:23 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08BDDC061764; Sat, 21 Aug 2021 11:48:43 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id u13-20020a17090abb0db0290177e1d9b3f7so16029357pjr.1; Sat, 21 Aug 2021 11:48:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=L3yT1j4pbUZknzUyxpuqzNzLJ0XV1FOv6P3LRPoFRb8=; b=kjYLTwX502hDBTrCam1tUMaCd/Kz8wuaC+UnL76keURQYfvliHzN4gYDwfxSwe+6gq hJ8TAFliZFaj1kvBEa8XB0EAM+lTgR1wqDNK/w0rfXZxCdOY9MWPde+J2E6/wmOqeZRS 46KfTZcvHZ7PvL+i+xTWiEOgGUNDWOkyi+u4EODAowqYned0wvQoSwnpgT7/e6/Va0Rp YCmDuHMYIJpHk1ONRUc67E0S/vdkFklbgrXnr8g27wMVg8EGiRQifOUnoCAfCsXeO4xN 30NAHXK/frPXF1XgN4o7EmBqH69mxcwfLu+fhnBl/zTc8GFPZ1OxBkBOLoGaFbdfAXpx wzdQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=L3yT1j4pbUZknzUyxpuqzNzLJ0XV1FOv6P3LRPoFRb8=; b=oPdvM8hnmnIB2sBF9ooA7YlMwogMRrVkykhKNOAL36167wy637LqTRcx8o2fm7phYN MTZZLdRpjI/L0mpXyucKI0aefIlKVxQBzBI8Bb+va/QLiXNVw+/GRentDBoeCwEBXLch vJa4I8NB3I92zN0gPymq0PXp4aN+KCP0oNNquHbcz5hmGYYovU0uqP37mifD24F5gK19 6RAL1XxZ7qVetMa5AIRiBC1p7OqSMA2EeS8ufgiMOGvRK5z0IyeFV5ORcEod0bGGE0Hk 3qfS5FiuqpNQBv4RK6tz2S09cIHmYykRPjU8H1H0KpwnIvQEkqNK58JLvoO7qu0o97Oq RsvA== X-Gm-Message-State: AOAM531eCbgoEjy76WrJ2WPIq/d2j0K/dzW1GsHX3GaBdCvyDfXtGf2b Zi6GZ0oMTRKAinYfsWIRdGZouf6aSPs= X-Google-Smtp-Source: ABdhPJwaadqsaDwiJ3bj/J+WXvcrD0kg+M6rGpLk2rVA+Ar8Q2vk1q4kN7CmCJvzCp3RnC9Fe1W/EA== X-Received: by 2002:a17:903:20c4:b0:12d:c7de:3401 with SMTP id i4-20020a17090320c400b0012dc7de3401mr21782084plb.40.1629571722416; Sat, 21 Aug 2021 11:48:42 -0700 (PDT) Received: from localhost ([2405:201:6014:d820:9cc6:d37f:c2fd:dc6]) by smtp.gmail.com with ESMTPSA id m28sm13462557pgl.9.2021.08.21.11.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 11:48:42 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , KP Singh , Spencer Baugh , Andy Lutomirski , Pavel Emelyanov , Alexander Mihalicyn , Andrei Vagin , linux-security-module@vger.kernel.org Subject: [PATCH bpf-next RFC v1 5/5] tools: testing: Add selftest for file local storage map Date: Sun, 22 Aug 2021 00:18:24 +0530 Message-Id: <20210821184824.2052643-6-memxor@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821184824.2052643-1-memxor@gmail.com> References: <20210821184824.2052643-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: This adds a test case for verifying that file local storage map works as intended. Signed-off-by: Kumar Kartikeya Dwivedi --- .../bpf/prog_tests/test_local_storage.c | 51 +++++++++++++++++++ .../selftests/bpf/progs/local_storage.c | 23 +++++++++ 2 files changed, 74 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c index d2c16eaae367..154dee32320c 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c +++ b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c @@ -24,6 +24,7 @@ static inline int sys_pidfd_open(pid_t pid, unsigned int flags) static unsigned int duration; #define TEST_STORAGE_VALUE 0xbeefdead +#define DUMMY_STORAGE_VALUE 0xdeadbeef struct storage { void *inode; @@ -111,6 +112,51 @@ static bool check_syscall_operations(int map_fd, int obj_fd) return true; } +int test_file_local_storage(struct bpf_map *map) +{ + struct storage ls; + int fd, ret; + + fd = open("/dev/null", O_RDONLY); + if (!ASSERT_GE(fd, 0, "open(/dev/null)")) + return -errno; + + ret = bpf_map_lookup_elem(bpf_map__fd(map), &fd, &ls); + if (!ASSERT_OK(ret, "bpf_map_lookup_elem for file local storage")) + goto end; + + ASSERT_EQ(ls.value, DUMMY_STORAGE_VALUE, "file local value match"); + + ret = bpf_map_delete_elem(bpf_map__fd(map), &fd); + if (!ASSERT_OK(ret, "bpf_map_delete_elem for file local storage")) + goto end; + + ret = bpf_map_lookup_elem(bpf_map__fd(map), &fd, &ls); + if (!ASSERT_EQ(ret, -ENOENT, "bpf_map_lookup_elem should fail")) + goto end; + + memset(&ls, 0, sizeof(ls)); + ls.value = DUMMY_STORAGE_VALUE; + ret = bpf_map_update_elem(bpf_map__fd(map), &fd, &ls, BPF_NOEXIST); + if (!ASSERT_OK(ret, "bpf_map_update_elem for file local storage")) + goto end; + + ret = bpf_map_lookup_elem(bpf_map__fd(map), &fd, &ls); + if (!ASSERT_OK(ret, "bpf_map_lookup_elem for file local storage")) + goto end; + + close(fd); + + ret = bpf_map_lookup_elem(bpf_map__fd(map), &fd, &ls); + if (!ASSERT_EQ(ret, -EBADF, "bpf_map_lookup_elem should fail")) + return -EINVAL; + + return 0; +end: + close(fd); + return ret; +} + void test_test_local_storage(void) { char tmp_dir_path[] = "/tmp/local_storageXXXXXX"; @@ -167,6 +213,11 @@ void test_test_local_storage(void) /* Set the process being monitored to be the current process */ skel->bss->monitored_pid = getpid(); + /* Test file local storage */ + err = test_file_local_storage(skel->maps.file_storage_map); + if (!ASSERT_OK(err, "test_file_local_storage")) + goto close_prog_rmdir; + /* Move copy_of_rm to a new location so that it triggers the * inode_rename LSM hook with a new_dentry that has a NULL inode ptr. */ diff --git a/tools/testing/selftests/bpf/progs/local_storage.c b/tools/testing/selftests/bpf/progs/local_storage.c index 95868bc7ada9..68561812f454 100644 --- a/tools/testing/selftests/bpf/progs/local_storage.c +++ b/tools/testing/selftests/bpf/progs/local_storage.c @@ -44,6 +44,13 @@ struct { __type(value, struct local_storage); } task_storage_map SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_FILE_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct local_storage); +} file_storage_map SEC(".maps"); + SEC("lsm/inode_unlink") int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim) { @@ -181,3 +188,19 @@ void BPF_PROG(exec, struct linux_binprm *bprm) storage->value = DUMMY_STORAGE_VALUE; bpf_spin_unlock(&storage->lock); } + +SEC("lsm/file_open") +int BPF_PROG(file_open, struct file *file) +{ + __u32 pid = bpf_get_current_pid_tgid() >> 32; + struct local_storage *storage; + + if (pid != monitored_pid) + return 0; + + storage = bpf_file_storage_get(&file_storage_map, file, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (storage) + storage->value = DUMMY_STORAGE_VALUE; + return 0; +}