Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
net: Guard BPF_GET_COMM_HASH uapi for MIUI builds
Browse files Browse the repository at this point in the history
Commit: 55fc8bd1edbc0de0ec7c18118ae2c0cb5e92f0b7
BPF_GET_COMM_HASH seems to be breaking android13 bpf loader.
Therefore, disable it for non MIUI builds to allow bpf loader
to boot on android13 ROMs.

Change-Id: I9f5c74760a75221d5ee8c8ead6c6f84a2dd5f812
Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
  • Loading branch information
UtsavBalar1231 authored and KaguraRinko committed Sep 3, 2022
1 parent 5324874 commit f1facc1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ struct sock {
u32 sk_ack_backlog;
u32 sk_max_ack_backlog;
kuid_t sk_uid;
#if IS_ENABLED(CONFIG_MIHW)
pid_t pid_num;
#endif
struct pid *sk_peer_pid;
const struct cred *sk_peer_cred;
long sk_rcvtimeo;
Expand Down
4 changes: 4 additions & 0 deletions include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ enum bpf_cmd {
BPF_OBJ_GET,
BPF_PROG_ATTACH,
BPF_PROG_DETACH,
#if IS_ENABLED(CONFIG_MIHW)
BPF_GET_COMM_HASH,
#endif
BPF_PROG_TEST_RUN,
BPF_PROG_GET_NEXT_ID,
BPF_MAP_GET_NEXT_ID,
Expand Down Expand Up @@ -413,10 +415,12 @@ union bpf_attr {
__u64 probe_addr; /* output: probe_addr */
} task_fd_query;

#if IS_ENABLED(CONFIG_MIHW)
struct { /* anonymous struct used by BPF_GET_COMM_HASH/DETACH commands */
__aligned_u64 hash; /* the hash of process comm */
__u32 pid; /* the pid of the process */;
};
#endif
} __attribute__((aligned(8)));

/* The description below is an attempt at providing documentation to eBPF
Expand Down
4 changes: 4 additions & 0 deletions kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
return err;
}

#if IS_ENABLED(CONFIG_MIHW)
static int bpf_get_comm_hash(union bpf_attr *attr)
{
void __user *uhash = u64_to_user_ptr(attr->hash);
Expand All @@ -2401,6 +2402,7 @@ static int bpf_get_comm_hash(union bpf_attr *attr)
return -EFAULT;
return 0;
}
#endif

SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
{
Expand Down Expand Up @@ -2490,9 +2492,11 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
case BPF_TASK_FD_QUERY:
err = bpf_task_fd_query(&attr, uattr);
break;
#if IS_ENABLED(CONFIG_MIHW)
case BPF_GET_COMM_HASH:
err = bpf_get_comm_hash(&attr);
break;
#endif
default:
err = -EINVAL;
break;
Expand Down
27 changes: 18 additions & 9 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3595,6 +3595,16 @@ BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
return err;
}

static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
.func = bpf_skb_get_tunnel_opt,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
.arg3_type = ARG_CONST_SIZE,
};

#if IS_ENABLED(CONFIG_MIHW)
/*
* simple hash function for a string,
* http://www.cse.yorku.ca/~oz/hash.html
Expand Down Expand Up @@ -3626,6 +3636,12 @@ BPF_CALL_1(bpf_get_comm_hash_from_sk, struct sk_buff *, skb)
rcu_read_unlock();
return hash;
}
#else
BPF_CALL_1(bpf_get_comm_hash_from_sk, struct sk_buff *, skb)
{
return 0;
}
#endif

static const struct bpf_func_proto bpf_get_comm_hash_from_sk_proto = {
.func = bpf_get_comm_hash_from_sk,
Expand All @@ -3634,15 +3650,6 @@ static const struct bpf_func_proto bpf_get_comm_hash_from_sk_proto = {
.arg1_type = ARG_PTR_TO_CTX,
};

static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
.func = bpf_skb_get_tunnel_opt,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
.arg3_type = ARG_CONST_SIZE,
};

static struct metadata_dst __percpu *md_dst;

BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
Expand Down Expand Up @@ -4946,8 +4953,10 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_get_socket_cookie_proto;
case BPF_FUNC_get_socket_uid:
return &bpf_get_socket_uid_proto;
#if IS_ENABLED(CONFIG_MIHW)
case BPF_FUNC_get_comm_hash_from_sk:
return &bpf_get_comm_hash_from_sk_proto;
#endif
default:
return bpf_base_func_proto(func_id);
}
Expand Down
2 changes: 2 additions & 0 deletions net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2802,7 +2802,9 @@ void sock_init_data(struct socket *sock, struct sock *sk)
sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0);
}

#if IS_ENABLED(CONFIG_MIHW)
sk->pid_num = pid_nr_ns(task_tgid(current), &init_pid_ns);
#endif
rwlock_init(&sk->sk_callback_lock);
if (sk->sk_kern_sock)
lockdep_set_class_and_name(
Expand Down

0 comments on commit f1facc1

Please sign in to comment.