Commit 7f677633 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by David S. Miller

bpf: introduce BPF_F_ALLOW_OVERRIDE flag

If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
to the given cgroup the descendent cgroup will be able to override
effective bpf program that was inherited from this cgroup.
By default it's not passed, therefore override is disallowed.

Examples:
1.
prog X attached to /A with default
prog Y fails to attach to /A/B and /A/B/C
Everything under /A runs prog X

2.
prog X attached to /A with allow_override.
prog Y fails to attach to /A/B with default (non-override)
prog M attached to /A/B with allow_override.
Everything under /A/B runs prog M only.

3.
prog X attached to /A with allow_override.
prog Y fails to attach to /A with default.
The user has to detach first to switch the mode.

In the future this behavior may be extended with a chain of
non-overridable programs.

Also fix the bug where detach from cgroup where nothing is attached
was not throwing error. Return ENOENT in such case.

Add several testcases and adjust libbpf.

Fixes: 30070984 ("cgroup: add support for eBPF programs")
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarDaniel Mack <daniel@zonque.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent e722af63
...@@ -21,20 +21,19 @@ struct cgroup_bpf { ...@@ -21,20 +21,19 @@ struct cgroup_bpf {
*/ */
struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE]; struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE];
struct bpf_prog __rcu *effective[MAX_BPF_ATTACH_TYPE]; struct bpf_prog __rcu *effective[MAX_BPF_ATTACH_TYPE];
bool disallow_override[MAX_BPF_ATTACH_TYPE];
}; };
void cgroup_bpf_put(struct cgroup *cgrp); void cgroup_bpf_put(struct cgroup *cgrp);
void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent); void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent);
void __cgroup_bpf_update(struct cgroup *cgrp, int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,
struct cgroup *parent, struct bpf_prog *prog, enum bpf_attach_type type,
struct bpf_prog *prog, bool overridable);
enum bpf_attach_type type);
/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ /* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */
void cgroup_bpf_update(struct cgroup *cgrp, int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,
struct bpf_prog *prog, enum bpf_attach_type type, bool overridable);
enum bpf_attach_type type);
int __cgroup_bpf_run_filter_skb(struct sock *sk, int __cgroup_bpf_run_filter_skb(struct sock *sk,
struct sk_buff *skb, struct sk_buff *skb,
......
...@@ -116,6 +116,12 @@ enum bpf_attach_type { ...@@ -116,6 +116,12 @@ enum bpf_attach_type {
#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
/* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
* to the given target_fd cgroup the descendent cgroup will be able to
* override effective bpf program that was inherited from this cgroup
*/
#define BPF_F_ALLOW_OVERRIDE (1U << 0)
#define BPF_PSEUDO_MAP_FD 1 #define BPF_PSEUDO_MAP_FD 1
/* flags for BPF_MAP_UPDATE_ELEM command */ /* flags for BPF_MAP_UPDATE_ELEM command */
...@@ -171,6 +177,7 @@ union bpf_attr { ...@@ -171,6 +177,7 @@ union bpf_attr {
__u32 target_fd; /* container object to attach to */ __u32 target_fd; /* container object to attach to */
__u32 attach_bpf_fd; /* eBPF program to attach */ __u32 attach_bpf_fd; /* eBPF program to attach */
__u32 attach_type; __u32 attach_type;
__u32 attach_flags;
}; };
} __attribute__((aligned(8))); } __attribute__((aligned(8)));
......
...@@ -52,6 +52,7 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) ...@@ -52,6 +52,7 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent)
e = rcu_dereference_protected(parent->bpf.effective[type], e = rcu_dereference_protected(parent->bpf.effective[type],
lockdep_is_held(&cgroup_mutex)); lockdep_is_held(&cgroup_mutex));
rcu_assign_pointer(cgrp->bpf.effective[type], e); rcu_assign_pointer(cgrp->bpf.effective[type], e);
cgrp->bpf.disallow_override[type] = parent->bpf.disallow_override[type];
} }
} }
...@@ -82,30 +83,63 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) ...@@ -82,30 +83,63 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent)
* *
* Must be called with cgroup_mutex held. * Must be called with cgroup_mutex held.
*/ */
void __cgroup_bpf_update(struct cgroup *cgrp, int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,
struct cgroup *parent, struct bpf_prog *prog, enum bpf_attach_type type,
struct bpf_prog *prog, bool new_overridable)
enum bpf_attach_type type)
{ {
struct bpf_prog *old_prog, *effective; struct bpf_prog *old_prog, *effective = NULL;
struct cgroup_subsys_state *pos; struct cgroup_subsys_state *pos;
bool overridable = true;
if (parent) {
overridable = !parent->bpf.disallow_override[type];
effective = rcu_dereference_protected(parent->bpf.effective[type],
lockdep_is_held(&cgroup_mutex));
}
old_prog = xchg(cgrp->bpf.prog + type, prog); if (prog && effective && !overridable)
/* if parent has non-overridable prog attached, disallow
* attaching new programs to descendent cgroup
*/
return -EPERM;
if (prog && effective && overridable != new_overridable)
/* if parent has overridable prog attached, only
* allow overridable programs in descendent cgroup
*/
return -EPERM;
effective = (!prog && parent) ? old_prog = cgrp->bpf.prog[type];
rcu_dereference_protected(parent->bpf.effective[type],
lockdep_is_held(&cgroup_mutex)) : if (prog) {
prog; overridable = new_overridable;
effective = prog;
if (old_prog &&
cgrp->bpf.disallow_override[type] == new_overridable)
/* disallow attaching non-overridable on top
* of existing overridable in this cgroup
* and vice versa
*/
return -EPERM;
}
if (!prog && !old_prog)
/* report error when trying to detach and nothing is attached */
return -ENOENT;
cgrp->bpf.prog[type] = prog;
css_for_each_descendant_pre(pos, &cgrp->self) { css_for_each_descendant_pre(pos, &cgrp->self) {
struct cgroup *desc = container_of(pos, struct cgroup, self); struct cgroup *desc = container_of(pos, struct cgroup, self);
/* skip the subtree if the descendant has its own program */ /* skip the subtree if the descendant has its own program */
if (desc->bpf.prog[type] && desc != cgrp) if (desc->bpf.prog[type] && desc != cgrp) {
pos = css_rightmost_descendant(pos); pos = css_rightmost_descendant(pos);
else } else {
rcu_assign_pointer(desc->bpf.effective[type], rcu_assign_pointer(desc->bpf.effective[type],
effective); effective);
desc->bpf.disallow_override[type] = !overridable;
}
} }
if (prog) if (prog)
...@@ -115,6 +149,7 @@ void __cgroup_bpf_update(struct cgroup *cgrp, ...@@ -115,6 +149,7 @@ void __cgroup_bpf_update(struct cgroup *cgrp,
bpf_prog_put(old_prog); bpf_prog_put(old_prog);
static_branch_dec(&cgroup_bpf_enabled_key); static_branch_dec(&cgroup_bpf_enabled_key);
} }
return 0;
} }
/** /**
......
...@@ -920,13 +920,14 @@ static int bpf_obj_get(const union bpf_attr *attr) ...@@ -920,13 +920,14 @@ static int bpf_obj_get(const union bpf_attr *attr)
#ifdef CONFIG_CGROUP_BPF #ifdef CONFIG_CGROUP_BPF
#define BPF_PROG_ATTACH_LAST_FIELD attach_type #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
static int bpf_prog_attach(const union bpf_attr *attr) static int bpf_prog_attach(const union bpf_attr *attr)
{ {
enum bpf_prog_type ptype;
struct bpf_prog *prog; struct bpf_prog *prog;
struct cgroup *cgrp; struct cgroup *cgrp;
enum bpf_prog_type ptype; int ret;
if (!capable(CAP_NET_ADMIN)) if (!capable(CAP_NET_ADMIN))
return -EPERM; return -EPERM;
...@@ -934,6 +935,9 @@ static int bpf_prog_attach(const union bpf_attr *attr) ...@@ -934,6 +935,9 @@ static int bpf_prog_attach(const union bpf_attr *attr)
if (CHECK_ATTR(BPF_PROG_ATTACH)) if (CHECK_ATTR(BPF_PROG_ATTACH))
return -EINVAL; return -EINVAL;
if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
return -EINVAL;
switch (attr->attach_type) { switch (attr->attach_type) {
case BPF_CGROUP_INET_INGRESS: case BPF_CGROUP_INET_INGRESS:
case BPF_CGROUP_INET_EGRESS: case BPF_CGROUP_INET_EGRESS:
...@@ -956,10 +960,13 @@ static int bpf_prog_attach(const union bpf_attr *attr) ...@@ -956,10 +960,13 @@ static int bpf_prog_attach(const union bpf_attr *attr)
return PTR_ERR(cgrp); return PTR_ERR(cgrp);
} }
cgroup_bpf_update(cgrp, prog, attr->attach_type); ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
if (ret)
bpf_prog_put(prog);
cgroup_put(cgrp); cgroup_put(cgrp);
return 0; return ret;
} }
#define BPF_PROG_DETACH_LAST_FIELD attach_type #define BPF_PROG_DETACH_LAST_FIELD attach_type
...@@ -967,6 +974,7 @@ static int bpf_prog_attach(const union bpf_attr *attr) ...@@ -967,6 +974,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)
static int bpf_prog_detach(const union bpf_attr *attr) static int bpf_prog_detach(const union bpf_attr *attr)
{ {
struct cgroup *cgrp; struct cgroup *cgrp;
int ret;
if (!capable(CAP_NET_ADMIN)) if (!capable(CAP_NET_ADMIN))
return -EPERM; return -EPERM;
...@@ -982,7 +990,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) ...@@ -982,7 +990,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)
if (IS_ERR(cgrp)) if (IS_ERR(cgrp))
return PTR_ERR(cgrp); return PTR_ERR(cgrp);
cgroup_bpf_update(cgrp, NULL, attr->attach_type); ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
cgroup_put(cgrp); cgroup_put(cgrp);
break; break;
...@@ -990,7 +998,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) ...@@ -990,7 +998,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)
return -EINVAL; return -EINVAL;
} }
return 0; return ret;
} }
#endif /* CONFIG_CGROUP_BPF */ #endif /* CONFIG_CGROUP_BPF */
......
...@@ -6498,15 +6498,16 @@ static __init int cgroup_namespaces_init(void) ...@@ -6498,15 +6498,16 @@ static __init int cgroup_namespaces_init(void)
subsys_initcall(cgroup_namespaces_init); subsys_initcall(cgroup_namespaces_init);
#ifdef CONFIG_CGROUP_BPF #ifdef CONFIG_CGROUP_BPF
void cgroup_bpf_update(struct cgroup *cgrp, int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,
struct bpf_prog *prog, enum bpf_attach_type type, bool overridable)
enum bpf_attach_type type)
{ {
struct cgroup *parent = cgroup_parent(cgrp); struct cgroup *parent = cgroup_parent(cgrp);
int ret;
mutex_lock(&cgroup_mutex); mutex_lock(&cgroup_mutex);
__cgroup_bpf_update(cgrp, parent, prog, type); ret = __cgroup_bpf_update(cgrp, parent, prog, type, overridable);
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
return ret;
} }
#endif /* CONFIG_CGROUP_BPF */ #endif /* CONFIG_CGROUP_BPF */
......
...@@ -104,7 +104,7 @@ static int attach_filter(int cg_fd, int type, int verdict) ...@@ -104,7 +104,7 @@ static int attach_filter(int cg_fd, int type, int verdict)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ret = bpf_prog_attach(prog_fd, cg_fd, type); ret = bpf_prog_attach(prog_fd, cg_fd, type, 0);
if (ret < 0) { if (ret < 0) {
printf("Failed to attach prog to cgroup: '%s'\n", printf("Failed to attach prog to cgroup: '%s'\n",
strerror(errno)); strerror(errno));
......
...@@ -79,11 +79,12 @@ int main(int argc, char **argv) ...@@ -79,11 +79,12 @@ int main(int argc, char **argv)
if (join_cgroup(FOO)) if (join_cgroup(FOO))
goto err; goto err;
if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS)) { if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
log_err("Attaching prog to /foo"); log_err("Attaching prog to /foo");
goto err; goto err;
} }
printf("Attached DROP prog. This ping in cgroup /foo should fail...\n");
assert(system(PING_CMD) != 0); assert(system(PING_CMD) != 0);
/* Create cgroup /foo/bar, get fd, and join it */ /* Create cgroup /foo/bar, get fd, and join it */
...@@ -94,24 +95,27 @@ int main(int argc, char **argv) ...@@ -94,24 +95,27 @@ int main(int argc, char **argv)
if (join_cgroup(BAR)) if (join_cgroup(BAR))
goto err; goto err;
printf("Attached DROP prog. This ping in cgroup /foo/bar should fail...\n");
assert(system(PING_CMD) != 0); assert(system(PING_CMD) != 0);
if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS)) { if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
log_err("Attaching prog to /foo/bar"); log_err("Attaching prog to /foo/bar");
goto err; goto err;
} }
printf("Attached PASS prog. This ping in cgroup /foo/bar should pass...\n");
assert(system(PING_CMD) == 0); assert(system(PING_CMD) == 0);
if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) { if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
log_err("Detaching program from /foo/bar"); log_err("Detaching program from /foo/bar");
goto err; goto err;
} }
printf("Detached PASS from /foo/bar while DROP is attached to /foo.\n"
"This ping in cgroup /foo/bar should fail...\n");
assert(system(PING_CMD) != 0); assert(system(PING_CMD) != 0);
if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS)) { if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
log_err("Attaching prog to /foo/bar"); log_err("Attaching prog to /foo/bar");
goto err; goto err;
} }
...@@ -121,8 +125,60 @@ int main(int argc, char **argv) ...@@ -121,8 +125,60 @@ int main(int argc, char **argv)
goto err; goto err;
} }
printf("Attached PASS from /foo/bar and detached DROP from /foo.\n"
"This ping in cgroup /foo/bar should pass...\n");
assert(system(PING_CMD) == 0); assert(system(PING_CMD) == 0);
if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
log_err("Attaching prog to /foo/bar");
goto err;
}
if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
errno = 0;
log_err("Unexpected success attaching prog to /foo/bar");
goto err;
}
if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
log_err("Detaching program from /foo/bar");
goto err;
}
if (!bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
errno = 0;
log_err("Unexpected success in double detach from /foo");
goto err;
}
if (bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
log_err("Attaching non-overridable prog to /foo");
goto err;
}
if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
errno = 0;
log_err("Unexpected success attaching non-overridable prog to /foo/bar");
goto err;
}
if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
errno = 0;
log_err("Unexpected success attaching overridable prog to /foo/bar");
goto err;
}
if (!bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
errno = 0;
log_err("Unexpected success attaching overridable prog to /foo");
goto err;
}
if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
log_err("Attaching different non-overridable prog to /foo");
goto err;
}
goto out; goto out;
err: err:
...@@ -132,5 +188,9 @@ int main(int argc, char **argv) ...@@ -132,5 +188,9 @@ int main(int argc, char **argv)
close(foo); close(foo);
close(bar); close(bar);
cleanup_cgroup_environment(); cleanup_cgroup_environment();
if (!rc)
printf("PASS\n");
else
printf("FAIL\n");
return rc; return rc;
} }
...@@ -75,7 +75,7 @@ int main(int argc, char **argv) ...@@ -75,7 +75,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ret = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE); ret = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE, 0);
if (ret < 0) { if (ret < 0) {
printf("Failed to attach prog to cgroup: '%s'\n", printf("Failed to attach prog to cgroup: '%s'\n",
strerror(errno)); strerror(errno));
......
...@@ -55,7 +55,7 @@ int main(int argc, char **argv) ...@@ -55,7 +55,7 @@ int main(int argc, char **argv)
} }
ret = bpf_prog_attach(prog_fd[filter_id], cg_fd, ret = bpf_prog_attach(prog_fd[filter_id], cg_fd,
BPF_CGROUP_INET_SOCK_CREATE); BPF_CGROUP_INET_SOCK_CREATE, 0);
if (ret < 0) { if (ret < 0) {
printf("Failed to attach prog to cgroup: '%s'\n", printf("Failed to attach prog to cgroup: '%s'\n",
strerror(errno)); strerror(errno));
......
...@@ -168,7 +168,8 @@ int bpf_obj_get(const char *pathname) ...@@ -168,7 +168,8 @@ int bpf_obj_get(const char *pathname)
return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr)); return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
} }
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
unsigned int flags)
{ {
union bpf_attr attr; union bpf_attr attr;
...@@ -176,6 +177,7 @@ int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) ...@@ -176,6 +177,7 @@ int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
attr.target_fd = target_fd; attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd; attr.attach_bpf_fd = prog_fd;
attr.attach_type = type; attr.attach_type = type;
attr.attach_flags = flags;
return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)); return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
} }
......
...@@ -41,7 +41,8 @@ int bpf_map_delete_elem(int fd, void *key); ...@@ -41,7 +41,8 @@ int bpf_map_delete_elem(int fd, void *key);
int bpf_map_get_next_key(int fd, void *key, void *next_key); int bpf_map_get_next_key(int fd, void *key, void *next_key);
int bpf_obj_pin(int fd, const char *pathname); int bpf_obj_pin(int fd, const char *pathname);
int bpf_obj_get(const char *pathname); int bpf_obj_get(const char *pathname);
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
unsigned int flags);
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment