Skip to content

Commit ea6932d

Browse files
changbindudavem330
authored andcommitted
net: make get_net_ns return error if NET_NS is disabled
There is a panic in socket ioctl cmd SIOCGSKNS when NET_NS is not enabled. The reason is that nsfs tries to access ns->ops but the proc_ns_operations is not implemented in this case. [7.670023] Unable to handle kernel NULL pointer dereference at virtual address 00000010 [7.670268] pgd = 32b54000 [7.670544] [00000010] *pgd=00000000 [7.671861] Internal error: Oops: 5 [#1] SMP ARM [7.672315] Modules linked in: [7.672918] CPU: 0 PID: 1 Comm: systemd Not tainted 5.13.0-rc3-00375-g6799d4f2da49 #16 [7.673309] Hardware name: Generic DT based system [7.673642] PC is at nsfs_evict+0x24/0x30 [7.674486] LR is at clear_inode+0x20/0x9c The same to tun SIOCGSKNS command. To fix this problem, we make get_net_ns() return -EINVAL when NET_NS is disabled. Meanwhile move it to right place net/core/net_namespace.c. Signed-off-by: Changbin Du <[email protected]> Fixes: c62cce2 ("net: add an ioctl to get a socket network namespace") Cc: Cong Wang <[email protected]> Cc: Jakub Kicinski <[email protected]> Cc: David Laight <[email protected]> Cc: Christian Brauner <[email protected]> Suggested-by: Jakub Kicinski <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1adb20f commit ea6932d

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

include/linux/socket.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,4 @@ extern int __sys_socketpair(int family, int type, int protocol,
438438
int __user *usockvec);
439439
extern int __sys_shutdown_sock(struct socket *sock, int how);
440440
extern int __sys_shutdown(int fd, int how);
441-
442-
extern struct ns_common *get_net_ns(struct ns_common *ns);
443441
#endif /* _LINUX_SOCKET_H */

include/net/net_namespace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
184184
void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
185185

186186
void net_ns_barrier(void);
187+
188+
struct ns_common *get_net_ns(struct ns_common *ns);
187189
#else /* CONFIG_NET_NS */
188190
#include <linux/sched.h>
189191
#include <linux/nsproxy.h>
@@ -203,6 +205,11 @@ static inline void net_ns_get_ownership(const struct net *net,
203205
}
204206

205207
static inline void net_ns_barrier(void) {}
208+
209+
static inline struct ns_common *get_net_ns(struct ns_common *ns)
210+
{
211+
return ERR_PTR(-EINVAL);
212+
}
206213
#endif /* CONFIG_NET_NS */
207214

208215

net/core/net_namespace.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,18 @@ void __put_net(struct net *net)
641641
}
642642
EXPORT_SYMBOL_GPL(__put_net);
643643

644+
/**
645+
* get_net_ns - increment the refcount of the network namespace
646+
* @ns: common namespace (net)
647+
*
648+
* Returns the net's common namespace.
649+
*/
650+
struct ns_common *get_net_ns(struct ns_common *ns)
651+
{
652+
return &get_net(container_of(ns, struct net, ns))->ns;
653+
}
654+
EXPORT_SYMBOL_GPL(get_net_ns);
655+
644656
struct net *get_net_ns_by_fd(int fd)
645657
{
646658
struct file *file;

net/socket.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,19 +1072,6 @@ static long sock_do_ioctl(struct net *net, struct socket *sock,
10721072
* what to do with it - that's up to the protocol still.
10731073
*/
10741074

1075-
/**
1076-
* get_net_ns - increment the refcount of the network namespace
1077-
* @ns: common namespace (net)
1078-
*
1079-
* Returns the net's common namespace.
1080-
*/
1081-
1082-
struct ns_common *get_net_ns(struct ns_common *ns)
1083-
{
1084-
return &get_net(container_of(ns, struct net, ns))->ns;
1085-
}
1086-
EXPORT_SYMBOL_GPL(get_net_ns);
1087-
10881075
static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
10891076
{
10901077
struct socket *sock;

0 commit comments

Comments
 (0)