Skip to content

Commit 2b73bc6

Browse files
nhormandavem330
authored andcommitted
netprio_cgroup: fix wrong memory access when NETPRIO_CGROUP=m
When the netprio_cgroup module is not loaded, net_prio_subsys_id is -1, and so sock_update_prioidx() accesses cgroup_subsys array with negative index subsys[-1]. Make the code resembles cls_cgroup code, which is bug free. Origionally-authored-by: Li Zefan <[email protected]> Signed-off-by: Li Zefan <[email protected]> Signed-off-by: Neil Horman <[email protected]> CC: "David S. Miller" <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f5c3820 commit 2b73bc6

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

include/net/netprio_cgroup.h

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,51 @@ extern int net_prio_subsys_id;
3737

3838
extern void sock_update_netprioidx(struct sock *sk);
3939

40-
static inline struct cgroup_netprio_state
41-
*task_netprio_state(struct task_struct *p)
40+
#if IS_BUILTIN(CONFIG_NETPRIO_CGROUP)
41+
42+
static inline u32 task_netprioidx(struct task_struct *p)
4243
{
43-
#if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
44-
return container_of(task_subsys_state(p, net_prio_subsys_id),
45-
struct cgroup_netprio_state, css);
46-
#else
47-
return NULL;
48-
#endif
44+
struct cgroup_netprio_state *state;
45+
u32 idx;
46+
47+
rcu_read_lock();
48+
state = container_of(task_subsys_state(p, net_prio_subsys_id),
49+
struct cgroup_netprio_state, css);
50+
idx = state->prioidx;
51+
rcu_read_unlock();
52+
return idx;
53+
}
54+
55+
#elif IS_MODULE(CONFIG_NETPRIO_CGROUP)
56+
57+
static inline u32 task_netprioidx(struct task_struct *p)
58+
{
59+
struct cgroup_netprio_state *state;
60+
int subsys_id;
61+
u32 idx = 0;
62+
63+
rcu_read_lock();
64+
subsys_id = rcu_dereference_index_check(net_prio_subsys_id,
65+
rcu_read_lock_held());
66+
if (subsys_id >= 0) {
67+
state = container_of(task_subsys_state(p, subsys_id),
68+
struct cgroup_netprio_state, css);
69+
idx = state->prioidx;
70+
}
71+
rcu_read_unlock();
72+
return idx;
4973
}
5074

5175
#else
5276

77+
static inline u32 task_netprioidx(struct task_struct *p)
78+
{
79+
return 0;
80+
}
81+
82+
#endif /* CONFIG_NETPRIO_CGROUP */
83+
84+
#else
5385
#define sock_update_netprioidx(sk)
5486
#endif
5587

net/core/sock.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,10 @@ EXPORT_SYMBOL(sock_update_classid);
11711171

11721172
void sock_update_netprioidx(struct sock *sk)
11731173
{
1174-
struct cgroup_netprio_state *state;
11751174
if (in_interrupt())
11761175
return;
1177-
rcu_read_lock();
1178-
state = task_netprio_state(current);
1179-
sk->sk_cgrp_prioidx = state ? state->prioidx : 0;
1180-
rcu_read_unlock();
1176+
1177+
sk->sk_cgrp_prioidx = task_netprioidx(current);
11811178
}
11821179
EXPORT_SYMBOL_GPL(sock_update_netprioidx);
11831180
#endif

0 commit comments

Comments
 (0)