Skip to content

Commit 04bba67

Browse files
Andrew Boiecarlescufi
authored andcommitted
net: sockets: add API to fetch an fd's ctx object
Zephyr running on MPU devices have a different memory model than process-oriented OSes like Linux and require a method to set kernel object permissions on a file descriptor's underlying context object. Add this, and a test to show that it is working. Signed-off-by: Andrew Boie <[email protected]>
1 parent fed960b commit 04bba67

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

include/net/socket.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,44 @@ struct zsock_addrinfo {
145145
char _ai_canonname[DNS_MAX_NAME_SIZE + 1];
146146
};
147147

148+
/**
149+
* @brief Obtain a file descriptor's associated net context
150+
*
151+
* With CONFIG_USERSPACE enabled, the kernel's object permission system
152+
* must apply to socket file descriptors. When a socket is opened, by default
153+
* only the caller has permission, access by other threads will fail unless
154+
* they have been specifically granted permission.
155+
*
156+
* This is achieved by tagging data structure definitions that implement the
157+
* underlying object associated with a network socket file descriptor with
158+
* '__net_socket`. All pointers to instances of these will be known to the
159+
* kernel as kernel objects with type K_OBJ_NET_SOCKET.
160+
*
161+
* This API is intended for threads that need to grant access to the object
162+
* associated with a particular file descriptor to another thread. The
163+
* returned pointer represents the underlying K_OBJ_NET_SOCKET and
164+
* may be passed to APIs like k_object_access_grant().
165+
*
166+
* In a system like Linux which has the notion of threads running in processes
167+
* in a shared virtual address space, this sort of management is unnecessary as
168+
* the scope of file descriptors is implemented at the process level.
169+
*
170+
* However in Zephyr the file descriptor scope is global, and MPU-based systems
171+
* are not able to implement a process-like model due to the lack of memory
172+
* virtualization hardware. They use discrete object permissions and memory
173+
* domains instead to define thread access scope.
174+
*
175+
* User threads will have no direct access to the returned object
176+
* and will fault if they try to access its memory; the pointer can only be
177+
* used to make permission assignment calls, which follow exactly the rules
178+
* for other kernel objects like device drivers and IPC.
179+
*
180+
* @param sock file descriptor
181+
* @return pointer to associated network socket object, or NULL if the
182+
* file descriptor wasn't valid or the caller had no access permission
183+
*/
184+
__syscall void *zsock_get_context_object(int sock);
185+
148186
/**
149187
* @brief Create a network socket
150188
*
@@ -156,6 +194,11 @@ struct zsock_addrinfo {
156194
* This function is also exposed as ``socket()``
157195
* if :option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
158196
* @endrst
197+
*
198+
* If CONFIG_USERSPACE is enabled, the caller will be granted access to the
199+
* context object associated with the returned file descriptor.
200+
* @see zsock_get_context_object()
201+
*
159202
*/
160203
__syscall int zsock_socket(int family, int type, int proto);
161204

subsys/net/lib/sockets/sockets.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ static inline void *get_sock_vtable(
4949
(const struct fd_op_vtable **)vtable);
5050
}
5151

52+
void *z_impl_zsock_get_context_object(int sock)
53+
{
54+
const struct socket_op_vtable *ignored;
55+
56+
return get_sock_vtable(sock, &ignored);
57+
}
58+
59+
#ifdef CONFIG_USERSPACE
60+
void *z_vrfy_zsock_get_context_object(int sock)
61+
{
62+
/* All checking done in implementation */
63+
return z_impl_zsock_get_context_object(sock);
64+
}
65+
66+
#include <syscalls/zsock_get_context_object_mrsh.c>
67+
#endif
68+
5269
static void zsock_received_cb(struct net_context *ctx,
5370
struct net_pkt *pkt,
5471
union net_ip_header *ip_hdr,

0 commit comments

Comments
 (0)