From patchwork Sat Apr 4 00:09:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 221449 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=-9.9 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, 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 BDF42C2BA15 for ; Sat, 4 Apr 2020 00:10:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8BEF120737 for ; Sat, 4 Apr 2020 00:10:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="ZDfpn7b2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726397AbgDDAKM (ORCPT ); Fri, 3 Apr 2020 20:10:12 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:58963 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726028AbgDDAKK (ORCPT ); Fri, 3 Apr 2020 20:10:10 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03409a6D015264 for ; Fri, 3 Apr 2020 17:10:08 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=X+5G5Mp6fY6EaFwnuc1B/XkP09ODoP08g6McOldy1hk=; b=ZDfpn7b2UsbD6YQyz3vqsOD7Jg+off7z0Gjh/XewKKeYqzZurO1AI8+YoU18qthHYjCD NFwkwULNjno2NXgyrS87Qq2o9eTZEYWnqZMD65tnm8SXoZ28N1Jv6Sv31FhdopTqlK1g +2KxPt71zUxSsCeK0vaFn5RvuY5IcOVfKxI= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 30697e9tfw-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 03 Apr 2020 17:10:08 -0700 Received: from intmgw005.03.ash8.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Fri, 3 Apr 2020 17:10:07 -0700 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id 5AE192EC2885; Fri, 3 Apr 2020 17:10:03 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Andrii Nakryiko Smtp-Origin-Hostname: devbig012.ftw2.facebook.com To: , , , CC: , , Andrii Nakryiko Smtp-Origin-Cluster: ftw2c04 Subject: [RFC PATCH bpf-next 2/8] bpf: allow bpf_link pinning as read-only and enforce LINK_UPDATE Date: Fri, 3 Apr 2020 17:09:41 -0700 Message-ID: <20200404000948.3980903-3-andriin@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200404000948.3980903-1-andriin@fb.com> References: <20200404000948.3980903-1-andriin@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-03_19:2020-04-03,2020-04-03 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 adultscore=0 spamscore=0 mlxscore=0 priorityscore=1501 clxscore=1015 phishscore=0 suspectscore=8 bulkscore=0 mlxlogscore=658 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004030183 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Make it possible to pin bpf_link as read-only to control whether LINK_UPDATE operation is allowed or not. bpf_links provided through read-only files are not allowed to perform LINK_UPDATE operations, which this patch starts enforcing. bpf_map and bpf_prog are still always treated as read-write ones, just like before. This is a critical property for bpf_links and is going to be relied upon for BPF_LINK_GET_FD_BY_ID operation implemented later in the series. GET_FD_BY_ID will only return read-only links to prevent processes that do not "own" bpf_link from updating underlying bpf_prog. Signed-off-by: Andrii Nakryiko --- include/linux/bpf.h | 6 +++--- kernel/bpf/inode.c | 30 ++++++++++++++++++++++-------- kernel/bpf/syscall.c | 26 +++++++++++++++++++------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ea65c3165e4c..3474f8e34a63 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1103,11 +1103,11 @@ void bpf_link_cleanup(struct bpf_link *link, struct file *link_file, int link_fd); void bpf_link_inc(struct bpf_link *link); void bpf_link_put(struct bpf_link *link); -int bpf_link_new_fd(struct bpf_link *link); +int bpf_link_new_fd(struct bpf_link *link, int flags); struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd); -struct bpf_link *bpf_link_get_from_fd(u32 ufd); +struct bpf_link *bpf_link_get_from_fd(u32 ufd, fmode_t *link_mode); -int bpf_obj_pin_user(u32 ufd, const char __user *pathname); +int bpf_obj_pin_user(u32 ufd, const char __user *pathname, int file_flags); int bpf_obj_get_user(const char __user *pathname, int flags); int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index 95087d9f4ed3..3fd71c1e3c33 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c @@ -66,23 +66,25 @@ static void bpf_any_put(void *raw, enum bpf_type type) } } -static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type) +static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type, fmode_t *file_mode) { void *raw; raw = bpf_map_get_with_uref(ufd); if (!IS_ERR(raw)) { *type = BPF_TYPE_MAP; + *file_mode = O_RDWR; return raw; } raw = bpf_prog_get(ufd); if (!IS_ERR(raw)) { *type = BPF_TYPE_PROG; + *file_mode = O_RDWR; return raw; } - raw = bpf_link_get_from_fd(ufd); + raw = bpf_link_get_from_fd(ufd, file_mode); if (!IS_ERR(raw)) { *type = BPF_TYPE_LINK; return raw; @@ -407,7 +409,7 @@ static const struct inode_operations bpf_dir_iops = { }; static int bpf_obj_do_pin(const char __user *pathname, void *raw, - enum bpf_type type) + enum bpf_type type, fmode_t file_mode) { struct dentry *dentry; struct inode *dir; @@ -419,7 +421,7 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw, if (IS_ERR(dentry)) return PTR_ERR(dentry); - mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask()); + mode = S_IFREG | (ACC_MODE(file_mode) & ~current_umask()); ret = security_path_mknod(&path, dentry, mode, 0); if (ret) @@ -449,17 +451,29 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw, return ret; } -int bpf_obj_pin_user(u32 ufd, const char __user *pathname) +int bpf_obj_pin_user(u32 ufd, const char __user *pathname, int file_flags) { enum bpf_type type; + fmode_t file_mode; void *raw; int ret; - raw = bpf_fd_probe_obj(ufd, &type); + raw = bpf_fd_probe_obj(ufd, &type, &file_mode); if (IS_ERR(raw)) return PTR_ERR(raw); - ret = bpf_obj_do_pin(pathname, raw, type); + if ((type == BPF_TYPE_MAP || type == BPF_TYPE_PROG) && file_flags) + return -EINVAL; + + /* requested pinned file mode has to be a valid subset */ + if (!file_flags) { + file_flags = file_mode; + } else if ((file_mode & file_flags) != file_flags) { + bpf_any_put(raw, type); + return -EPERM; + } + + ret = bpf_obj_do_pin(pathname, raw, type, file_flags); if (ret != 0) bpf_any_put(raw, type); @@ -518,7 +532,7 @@ int bpf_obj_get_user(const char __user *pathname, int flags) else if (type == BPF_TYPE_MAP) ret = bpf_map_new_fd(raw, f_flags); else if (type == BPF_TYPE_LINK) - ret = bpf_link_new_fd(raw); + ret = bpf_link_new_fd(raw, f_flags); else return -ENOENT; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 40993d8c936e..47f323901ed9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2167,10 +2167,11 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) static int bpf_obj_pin(const union bpf_attr *attr) { - if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) + if (CHECK_ATTR(BPF_OBJ)) return -EINVAL; - return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); + return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname), + attr->file_flags); } static int bpf_obj_get(const union bpf_attr *attr) @@ -2294,9 +2295,10 @@ const struct file_operations bpf_link_fops = { .write = bpf_dummy_write, }; -int bpf_link_new_fd(struct bpf_link *link) +int bpf_link_new_fd(struct bpf_link *link, int flags) { - return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); + return anon_inode_getfd("bpf-link", &bpf_link_fops, link, + flags | O_CLOEXEC); } /* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but @@ -2316,7 +2318,8 @@ struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd) if (fd < 0) return ERR_PTR(fd); - file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); + file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, + O_RDWR | O_CLOEXEC); if (IS_ERR(file)) { put_unused_fd(fd); return file; @@ -2326,7 +2329,7 @@ struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd) return file; } -struct bpf_link *bpf_link_get_from_fd(u32 ufd) +struct bpf_link *bpf_link_get_from_fd(u32 ufd, fmode_t *link_mode) { struct fd f = fdget(ufd); struct bpf_link *link; @@ -2340,6 +2343,8 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd) link = f.file->private_data; bpf_link_inc(link); + if (link_mode) + *link_mode = f.file->f_mode; fdput(f); return link; @@ -3612,6 +3617,7 @@ static int link_update(union bpf_attr *attr) { struct bpf_prog *old_prog = NULL, *new_prog; struct bpf_link *link; + fmode_t link_mode; u32 flags; int ret; @@ -3625,10 +3631,16 @@ static int link_update(union bpf_attr *attr) if (flags & ~BPF_F_REPLACE) return -EINVAL; - link = bpf_link_get_from_fd(attr->link_update.link_fd); + link = bpf_link_get_from_fd(attr->link_update.link_fd, &link_mode); if (IS_ERR(link)) return PTR_ERR(link); + /* read-only link references are not allowed to perform LINK_UPDATE */ + if (!(link_mode & O_WRONLY)) { + bpf_link_put(link); + return -EACCES; + } + new_prog = bpf_prog_get(attr->link_update.new_prog_fd); if (IS_ERR(new_prog)) return PTR_ERR(new_prog);