Skip to contentSkip to conversation

Fix control messages in sys_rtdm_recvmsg

PATCH New Manuel Huber 1 participant 4 messages Oct 2, 2013

lore
Share & tools
lore
#0 lore raw

cover Fix control messages in sys_rtdm_recvmsg

This patch-set shall fix the recvmsg system-call and its handling of control messages and the msghdr structure. It is necessary to be able to apply another patch to RTnet, which depends on a correct handling of msg_controllen.
Jan pointed out, that all driver code that relies on the current interface has to be adapted to the new one. I checked the driver code and I found can drivers to be the only ones affected. Finally, the changes to the system call have been documented in rtdm_driver.h header file. Please let me know if it's unclear...

I used a recent linux kernel as reference of how to implement the handling of msghdr. The patch changes the hadnling of msg_namelen as it won't get written back to user-space if it has been modified by driver code. That's what the Kernel does. However, there are also drivers in the Kernel tree that write to msg_namelen, so I'm not really sure what's the correct behaviour. So this might break user code that relies on this write-back although I can't really imagine a use-case where you actually need to check the size. Usually you have to know the address type (structure) to be able to interepret the address correctly.

Manuel Huber (3):
rtdm: Fix msghdr struct (cmsg) in sys_rtdm_recvmsg
rtcan: Adapt rtcan_raw to changes in rtdm
doc: Add documentation to rtdm_recvmsg_handler_t

 include/rtdm/rtdm_driver.h   |  9 ++++++++-
 ksrc/drivers/can/rtcan_raw.c | 33 ++++++++++++++++-----------------
 ksrc/skins/rtdm/syscall.c    | 18 ++++++++++++++----
 3 files changed, 38 insertions(+), 22 deletions(-)
Signature · 1 line
1.8.3
#1 lore raw

PATCH 1/3 rtdm: Fix msghdr struct (cmsg) in sys_rtdm_recvmsg

From: Manuel Huber <[email protected]>

Whenever a new control message is put into msg_control buffer the actual address and the space left is saved to msg_control and msg_controllen. This allows adding messages as long as there is enough space left in the user-supplied buffer. Both fields have to be fixed again before passing them to the user by copying the original starting address of the buffer to msg_control and saving the actual amount of bytes written to the buffer to msg_controllen.

Note: Changes of msg_namelen (in driver code) will not be reflected to user-space anymore. This change could break user-space applications that depend on the previous implementation.

* Explicit use of __xn_put_user rather then __xn_copy_to_user
* Don't write back msg->msg_namelen
* Reset msg_flags before passing msg to driver code

---
 ksrc/skins/rtdm/syscall.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
ksrc/skins/rtdm/syscall.c +14 -4
index 0ff5d40..055cf7c 100644
@@ -79,22 +79,32 @@ static int sys_rtdm_recvmsg(struct pt_regs *regs)
 {
 	struct task_struct *p = current;
 	struct msghdr krnl_msg;
+	uintptr_t cmsg_ptr;
+	struct msghdr __user *usr_msg;
 	int ret;
 
-	if (unlikely(!access_wok(__xn_reg_arg2(regs),
+	usr_msg = (void __user *)__xn_reg_arg2(regs);
+
+	if (unlikely(!access_wok((void __user *)usr_msg,
 				 sizeof(krnl_msg)) ||
 		     __xn_copy_from_user(&krnl_msg,
-					 (void __user *)__xn_reg_arg2(regs),
+					 (void __user *)usr_msg,
 					 sizeof(krnl_msg))))
 		return -EFAULT;
 
+	cmsg_ptr = (uintptr_t)krnl_msg.msg_control;
+	krnl_msg.msg_flags = 0;
+
 	ret = __rt_dev_recvmsg(p, __xn_reg_arg1(regs), &krnl_msg,
 			       __xn_reg_arg3(regs));
 	if (unlikely(ret < 0))
 		return ret;
 
-	if (unlikely(__xn_copy_to_user((void __user *)__xn_reg_arg2(regs),
-				       &krnl_msg, sizeof(krnl_msg))))
+	if (unlikely(__xn_put_user((typeof(krnl_msg.msg_controllen))
+				   ((uintptr_t)krnl_msg.msg_control - cmsg_ptr),
+				   (void __user *)&usr_msg->msg_controllen) ||
+		     __xn_put_user(krnl_msg.msg_flags,
+				   (void __user *)&(usr_msg->msg_flags))))
 		return -EFAULT;
 
 	return ret;
Signature · 1 line
1.8.3
#2 lore raw

PATCH 2/3 rtcan: Adapt rtcan_raw to changes in rtdm

In reply to Manuel Huber

From: Manuel Huber <[email protected]>

---
 ksrc/drivers/can/rtcan_raw.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
ksrc/drivers/can/rtcan_raw.c +16 -17
index ded024e..c244247 100644
@@ -559,6 +559,7 @@ ssize_t rtcan_raw_recvmsg(struct rtdm_dev_context *context,
     size_t payload_size;
     rtdm_lockctx_t lock_ctx;
     int ret;
+    int copy_timestamp = 0;
 
     /* Clear frame memory location */
     memset(&frame, 0, sizeof(can_frame_t));
@@ -610,8 +611,10 @@ ssize_t rtcan_raw_recvmsg(struct rtdm_dev_context *context,
     }
 
     if (msg->msg_control != NULL) {
-	if (msg->msg_controllen < sizeof(nanosecs_abs_t))
-	    return -EINVAL;
+	if (msg->msg_controllen >= sizeof(timestamp))
+	    copy_timestamp = 1;
+	else
+	    msg->msg_flags |= MSG_CTRUNC;
 
 	if (user_info) {
 	    if (!rtdm_rw_user_ok(user_info, msg->msg_control,
@@ -686,7 +689,7 @@ ssize_t rtcan_raw_recvmsg(struct rtdm_dev_context *context,
 
 
     /* Is a timestamp available and is the caller actually interested? */
-    if (msg->msg_controllen && (can_dlc & RTCAN_HAS_TIMESTAMP)) {
+    if (copy_timestamp && (can_dlc & RTCAN_HAS_TIMESTAMP)) {
 	/* Copy timestamp */
 	MEMCPY_FROM_RING_BUF(&timestamp, RTCAN_TIMESTAMP_SIZE);
     }
@@ -741,15 +744,13 @@ ssize_t rtcan_raw_recvmsg(struct rtdm_dev_context *context,
 	    return -EFAULT;
 
 	/* Copy timestamp if existent and wanted */
-	if (msg->msg_controllen) {
-	    if (can_dlc & RTCAN_HAS_TIMESTAMP) {
-		if (rtdm_copy_to_user(user_info, msg->msg_control,
-				      &timestamp, RTCAN_TIMESTAMP_SIZE))
-		    return -EFAULT;
+	if (copy_timestamp && (can_dlc & RTCAN_HAS_TIMESTAMP)) {
+	    if (rtdm_copy_to_user(user_info, msg->msg_control,
+				  &timestamp, RTCAN_TIMESTAMP_SIZE))
+		return -EFAULT;
 
-		msg->msg_controllen = RTCAN_TIMESTAMP_SIZE;
-	    } else
-		msg->msg_controllen = 0;
+	    msg->msg_controllen -= RTCAN_TIMESTAMP_SIZE;
+	    msg->msg_control += RTCAN_TIMESTAMP_SIZE;
 	}
 
     } else {
@@ -768,12 +769,10 @@ ssize_t rtcan_raw_recvmsg(struct rtdm_dev_context *context,
 	iov->iov_len -= sizeof(can_frame_t);
 
 	/* Copy timestamp if existent and wanted */
-	if (msg->msg_controllen) {
-	    if (can_dlc & RTCAN_HAS_TIMESTAMP) {
-		memcpy(msg->msg_control, &timestamp, RTCAN_TIMESTAMP_SIZE);
-		msg->msg_controllen = RTCAN_TIMESTAMP_SIZE;
-	    } else
-		msg->msg_controllen = 0;
+	if (copy_timestamp && (can_dlc & RTCAN_HAS_TIMESTAMP)) {
+	    memcpy(msg->msg_control, &timestamp, RTCAN_TIMESTAMP_SIZE);
+	    msg->msg_controllen -= RTCAN_TIMESTAMP_SIZE;
+	    msg->msg_control += RTCAN_TIMESTAMP_SIZE;
 	}
     }
 
Signature · 1 line
1.8.3
#3 lore raw

PATCH 3/3 doc: Add documentation to rtdm_recvmsg_handler_t

In reply to Manuel Huber

From: Manuel Huber <[email protected]>

Describes consequences of changes in sys_rtdm_recvmsg to the interface regarding the msghdr struct.

---
 include/rtdm/rtdm_driver.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
include/rtdm/rtdm_driver.h +8 -1
index 8b01f04..d8e3695 100644
@@ -294,7 +294,14 @@ typedef ssize_t (*rtdm_write_handler_t)(struct rtdm_dev_context *context,
  * @param[in] user_info Opaque pointer to information about user mode caller,
  * NULL if kernel mode call
  * @param[in,out] msg Message descriptor as passed by the user, automatically
- * mirrored to safe kernel memory in case of user mode call
+ * mirrored to safe kernel memory in case of user mode call. If msg_control
+ * points to a valid buffer (not NULL) and msg_controllen is > 0, cmsghdr
+ * structures can be put into msg_control buffer by driver code. Driver code
+ * has to move msg_control to point to the next free byte (or the end of the
+ * buffer). The core layer will update msg_controllen to reflect the actual
+ * number of meaningful bytes written to msg_control buffer. If there is
+ * insufficient space to write all control messages, MSG_CTRUNC has to be set
+ * by the driver (do not abort).
  * @param[in] flags Message flags as passed by the user
  *
  * @return On success, the number of bytes received. On failure return
Signature · 1 line
1.8.3