Skip to contentSkip to conversation

kcov: improve mmap processing

PATCH Superseded Aleksandr Nogikh 2 participants 5 messages Dec 21, 2021 – Dec 22, 2021 KCOV

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover kcov: improve mmap processing

Subsequent mmaps of the same kcov descriptor currently do not update the virtual memory of the task and yet return 0 (success). This is counter-intuitive and may lead to unexpected memory access errors.

Also, this unnecessarily limits the functionality of kcov to only the simplest usage scenarios. Kcov instances are effectively forever attached to their first address spaces and it becomes impossible to e.g. reuse the same kcov handle in forked child processes without mmapping the memory first. This is exactly what we tried to do in syzkaller and inadvertently came upon this behavior.

This patch series addresses the problem described above.

v1 of the patch:
https://lore.kernel.org/lkml/[email protected]/

Changes from v1:
- Split into 2 commits.
- Minor coding style changes.

Aleksandr Nogikh (2):
kcov: split ioctl handling into locked and unlocked parts
kcov: properly handle subsequent mmap calls

 kernel/kcov.c | 99 +++++++++++++++++++++++++++++----------------------
 1 file changed, 57 insertions(+), 42 deletions(-)
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#1 lore raw

PATCH v2 1/2 kcov: split ioctl handling into locked and unlocked parts

Currently all ioctls are de facto processed under a spin lock in order to serialise them. This, however, prohibits the use of vmalloc and other memory management functions in the implementation of those ioctls, unnecessary complicating any further changes.

Let all ioctls first be processed inside the kcov_ioctl_unlocked() function which should execute the ones that are not compatible with spinlock and pass control to kcov_ioctl_locked() for all other ones.

Although it is still compatible with a spinlock, move KCOV_INIT_TRACE handling to kcov_ioctl_unlocked(), so that its planned change is easier to follow.

---
 kernel/kcov.c | 64 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 25 deletions(-)
kernel/kcov.c +39 -25
index 36ca640c4f8e..5d87b4e0126f 100644
@@ -564,31 +564,12 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
 			     unsigned long arg)
 {
 	struct task_struct *t;
-	unsigned long size, unused;
+	unsigned long flags, unused;
 	int mode, i;
 	struct kcov_remote_arg *remote_arg;
 	struct kcov_remote *remote;
-	unsigned long flags;
 
 	switch (cmd) {
-	case KCOV_INIT_TRACE:
-		/*
-		 * Enable kcov in trace mode and setup buffer size.
-		 * Must happen before anything else.
-		 */
-		if (kcov->mode != KCOV_MODE_DISABLED)
-			return -EBUSY;
-		/*
-		 * Size must be at least 2 to hold current position and one PC.
-		 * Later we allocate size * sizeof(unsigned long) memory,
-		 * that must not overflow.
-		 */
-		size = arg;
-		if (size < 2 || size > INT_MAX / sizeof(unsigned long))
-			return -EINVAL;
-		kcov->size = size;
-		kcov->mode = KCOV_MODE_INIT;
-		return 0;
 	case KCOV_ENABLE:
 		/*
 		 * Enable coverage for the current task.
@@ -685,6 +666,43 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
 	}
 }
 
+static int kcov_ioctl_unlocked(struct kcov *kcov, unsigned int cmd,
+			     unsigned long arg)
+{
+	unsigned long size, flags;
+	int res;
+
+	switch (cmd) {
+	case KCOV_INIT_TRACE:
+		/*
+		 * Enable kcov in trace mode and setup buffer size.
+		 * Must happen before anything else.
+		 */
+		if (kcov->mode != KCOV_MODE_DISABLED)
+			return -EBUSY;
+		/*
+		 * Size must be at least 2 to hold current position and one PC.
+		 * Later we allocate size * sizeof(unsigned long) memory,
+		 * that must not overflow.
+		 */
+		size = arg;
+		if (size < 2 || size > INT_MAX / sizeof(unsigned long))
+			return -EINVAL;
+		kcov->size = size;
+		kcov->mode = KCOV_MODE_INIT;
+		return 0;
+	default:
+		/*
+		 * All other commands can be fully executed under a spin lock, so we
+		 * obtain and release it here to simplify the code of kcov_ioctl_locked().
+		 */
+		spin_lock_irqsave(&kcov->lock, flags);
+		res = kcov_ioctl_locked(kcov, cmd, arg);
+		spin_unlock_irqrestore(&kcov->lock, flags);
+		return res;
+	}
+}
+
 static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 {
 	struct kcov *kcov;
@@ -692,7 +710,6 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 	struct kcov_remote_arg *remote_arg = NULL;
 	unsigned int remote_num_handles;
 	unsigned long remote_arg_size;
-	unsigned long flags;
 
 	if (cmd == KCOV_REMOTE_ENABLE) {
 		if (get_user(remote_num_handles, (unsigned __user *)(arg +
@@ -713,10 +730,7 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 	}
 
 	kcov = filep->private_data;
-	spin_lock_irqsave(&kcov->lock, flags);
-	res = kcov_ioctl_locked(kcov, cmd, arg);
-	spin_unlock_irqrestore(&kcov->lock, flags);
-
+	res = kcov_ioctl_unlocked(kcov, cmd, arg);
 	kfree(remote_arg);
 
 	return res;
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#2 lore raw

Re: [PATCH v2 1/2] kcov: split ioctl handling into locked and unlocked parts

Quoted 75 lines · Aleksandr Nogikh

Currently all ioctls are de facto processed under a spin lock in order to serialise them. This, however, prohibits the use of vmalloc and other memory management functions in the implementation of those ioctls, unnecessary complicating any further changes.

Let all ioctls first be processed inside the kcov_ioctl_unlocked() function which should execute the ones that are not compatible with spinlock and pass control to kcov_ioctl_locked() for all other ones.

Although it is still compatible with a spinlock, move KCOV_INIT_TRACE handling to kcov_ioctl_unlocked(), so that its planned change is easier to follow.

---
 kernel/kcov.c | 64 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 25 deletions(-)
kernel/kcov.c +16 -20
index 36ca640c4f8e..5d87b4e0126f 100644
@@ -564,31 +564,12 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
                             unsigned long arg)
 {
        struct task_struct *t;
-       unsigned long size, unused;
+       unsigned long flags, unused;
        int mode, i;
        struct kcov_remote_arg *remote_arg;
        struct kcov_remote *remote;
-       unsigned long flags;
 
        switch (cmd) {
-       case KCOV_INIT_TRACE:
-               /*
-                * Enable kcov in trace mode and setup buffer size.
-                * Must happen before anything else.
-                */
-               if (kcov->mode != KCOV_MODE_DISABLED)
-                       return -EBUSY;
-               /*
-                * Size must be at least 2 to hold current position and one PC.
-                * Later we allocate size * sizeof(unsigned long) memory,
-                * that must not overflow.
-                */
-               size = arg;
-               if (size < 2 || size > INT_MAX / sizeof(unsigned long))
-                       return -EINVAL;
-               kcov->size = size;
-               kcov->mode = KCOV_MODE_INIT;
-               return 0;
        case KCOV_ENABLE:
                /*
                 * Enable coverage for the current task.
@@ -685,6 +666,43 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
        }
 }
 
+static int kcov_ioctl_unlocked(struct kcov *kcov, unsigned int cmd,
+                            unsigned long arg)
+{
+       unsigned long size, flags;
+       int res;
+
+       switch (cmd) {
+       case KCOV_INIT_TRACE:
+               /*
+                * Enable kcov in trace mode and setup buffer size.
+                * Must happen before anything else.
+                */
+               if (kcov->mode != KCOV_MODE_DISABLED)
+                       return -EBUSY;
+               /*

kernel/kcov.c static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, · line 693

Quoted 10 lines
kernel/kcov.c +10 -0
+                * Size must be at least 2 to hold current position and one PC.
+                * Later we allocate size * sizeof(unsigned long) memory,
+                * that must not overflow.
+                */
+               size = arg;
+               if (size < 2 || size > INT_MAX / sizeof(unsigned long))
+                       return -EINVAL;
+               kcov->size = size;
+               kcov->mode = KCOV_MODE_INIT;
+               return 0;

This patch should be a non-functional change, but it is not.

To do that, you'd have to add the locking around KCOV_INIT_TRACE here, and then do whatever else you're doing in patch 2/2.

Quoted 24 lines
kernel/kcov.c +12 -1
+       default:
+               /*
+                * All other commands can be fully executed under a spin lock, so we
+                * obtain and release it here to simplify the code of kcov_ioctl_locked().
+                */
+               spin_lock_irqsave(&kcov->lock, flags);
+               res = kcov_ioctl_locked(kcov, cmd, arg);
+               spin_unlock_irqrestore(&kcov->lock, flags);
+               return res;
+       }
+}
+
 static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 {
        struct kcov *kcov;
@@ -692,7 +710,6 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        struct kcov_remote_arg *remote_arg = NULL;
        unsigned int remote_num_handles;
        unsigned long remote_arg_size;
-       unsigned long flags;
 
        if (cmd == KCOV_REMOTE_ENABLE) {
                if (get_user(remote_num_handles, (unsigned __user *)(arg +

kernel/kcov.c static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) · line 733

Quoted 8 lines
kernel/kcov.c +1 -4
@@ -713,10 +730,7 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        }
 
        kcov = filep->private_data;
-       spin_lock_irqsave(&kcov->lock, flags);
-       res = kcov_ioctl_locked(kcov, cmd, arg);
-       spin_unlock_irqrestore(&kcov->lock, flags);
-
+       res = kcov_ioctl_unlocked(kcov, cmd, arg);

Also, I find that kcov_ioctl_unlocked() isn't a very descriptive name, since now we have both locked and unlocked variants. What is it actually doing?

Perhaps kcov_ioctl_with_context()? Assuming that 'struct kcov' is some sort of context.

Quoted 6 lines
kernel/kcov.c +0 -1
        kfree(remote_arg);
 
        return res;
--

2.34.1.307.g9b7440fafd-goog

#3 lore raw

Re: [PATCH v2 1/2] kcov: split ioctl handling into locked and unlocked parts

Quoted 1 line

To do that, you'd have to add the locking around KCOV_INIT_TRACE here,

Argh, indeed! Thanks, I'll fix it in v3.

Quoted 1 line

Also, I find that kcov_ioctl_unlocked() isn't a very descriptive name,

since now we have both locked and unlocked variants. What is it actually doing?

The main motivation behind introducing that function was to get the ability to do some processing outside of a spin lock without major code refactoring. So it kind of wraps the existing ioctl processing and, if it's KCOV_INIT_TRACE, performs the action itself.

I'm now thinking that we could probably do without introducing an extra function at all - by moving spin_lock_irqsave (&kcov->lock, flags); and spin_unlock_irqrestore(&kcov->lock, flags); into the kcov_ioctl_locked function (and rename it into sth like kcov_do_ioctl). So it could look like this:

switch (cmd) {
case KCOV_INIT_TRACE:
//...
}
spin_lock_irqsave (&kcov->lock, kcov_flags);
switch (cmd) {
case KCOV_ENABLE:
//...
default:
//...
}
spin_unlock_irqrestore(&kcov->lock, kcov_flags);

Quoted 139 lines · Marco Elver
Quoted 75 lines · Aleksandr Nogikh

Currently all ioctls are de facto processed under a spin lock in order to serialise them. This, however, prohibits the use of vmalloc and other memory management functions in the implementation of those ioctls, unnecessary complicating any further changes.

Let all ioctls first be processed inside the kcov_ioctl_unlocked() function which should execute the ones that are not compatible with spinlock and pass control to kcov_ioctl_locked() for all other ones.

Although it is still compatible with a spinlock, move KCOV_INIT_TRACE handling to kcov_ioctl_unlocked(), so that its planned change is easier to follow.

---
 kernel/kcov.c | 64 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 25 deletions(-)
kernel/kcov.c +16 -20
index 36ca640c4f8e..5d87b4e0126f 100644
@@ -564,31 +564,12 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
                             unsigned long arg)
 {
        struct task_struct *t;
-       unsigned long size, unused;
+       unsigned long flags, unused;
        int mode, i;
        struct kcov_remote_arg *remote_arg;
        struct kcov_remote *remote;
-       unsigned long flags;
 
        switch (cmd) {
-       case KCOV_INIT_TRACE:
-               /*
-                * Enable kcov in trace mode and setup buffer size.
-                * Must happen before anything else.
-                */
-               if (kcov->mode != KCOV_MODE_DISABLED)
-                       return -EBUSY;
-               /*
-                * Size must be at least 2 to hold current position and one PC.
-                * Later we allocate size * sizeof(unsigned long) memory,
-                * that must not overflow.
-                */
-               size = arg;
-               if (size < 2 || size > INT_MAX / sizeof(unsigned long))
-                       return -EINVAL;
-               kcov->size = size;
-               kcov->mode = KCOV_MODE_INIT;
-               return 0;
        case KCOV_ENABLE:
                /*
                 * Enable coverage for the current task.
@@ -685,6 +666,43 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
        }
 }
 
+static int kcov_ioctl_unlocked(struct kcov *kcov, unsigned int cmd,
+                            unsigned long arg)
+{
+       unsigned long size, flags;
+       int res;
+
+       switch (cmd) {
+       case KCOV_INIT_TRACE:
+               /*
+                * Enable kcov in trace mode and setup buffer size.
+                * Must happen before anything else.
+                */
+               if (kcov->mode != KCOV_MODE_DISABLED)
+                       return -EBUSY;
+               /*

kernel/kcov.c static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, · line 693

Quoted 10 lines
kernel/kcov.c +10 -0
+                * Size must be at least 2 to hold current position and one PC.
+                * Later we allocate size * sizeof(unsigned long) memory,
+                * that must not overflow.
+                */
+               size = arg;
+               if (size < 2 || size > INT_MAX / sizeof(unsigned long))
+                       return -EINVAL;
+               kcov->size = size;
+               kcov->mode = KCOV_MODE_INIT;
+               return 0;

This patch should be a non-functional change, but it is not.

To do that, you'd have to add the locking around KCOV_INIT_TRACE here, and then do whatever else you're doing in patch 2/2.

Quoted 24 lines
kernel/kcov.c +12 -1
+       default:
+               /*
+                * All other commands can be fully executed under a spin lock, so we
+                * obtain and release it here to simplify the code of kcov_ioctl_locked().
+                */
+               spin_lock_irqsave(&kcov->lock, flags);
+               res = kcov_ioctl_locked(kcov, cmd, arg);
+               spin_unlock_irqrestore(&kcov->lock, flags);
+               return res;
+       }
+}
+
 static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 {
        struct kcov *kcov;
@@ -692,7 +710,6 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        struct kcov_remote_arg *remote_arg = NULL;
        unsigned int remote_num_handles;
        unsigned long remote_arg_size;
-       unsigned long flags;
 
        if (cmd == KCOV_REMOTE_ENABLE) {
                if (get_user(remote_num_handles, (unsigned __user *)(arg +

kernel/kcov.c static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) · line 733

Quoted 8 lines
kernel/kcov.c +1 -4
@@ -713,10 +730,7 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        }
 
        kcov = filep->private_data;
-       spin_lock_irqsave(&kcov->lock, flags);
-       res = kcov_ioctl_locked(kcov, cmd, arg);
-       spin_unlock_irqrestore(&kcov->lock, flags);
-
+       res = kcov_ioctl_unlocked(kcov, cmd, arg);

Also, I find that kcov_ioctl_unlocked() isn't a very descriptive name, since now we have both locked and unlocked variants. What is it actually doing?

Perhaps kcov_ioctl_with_context()? Assuming that 'struct kcov' is some sort of context.

Quoted 6 lines
kernel/kcov.c +0 -1
        kfree(remote_arg);
 
        return res;
--

2.34.1.307.g9b7440fafd-goog

#4 lore raw

PATCH v2 2/2 kcov: properly handle subsequent mmap calls

In reply to Aleksandr Nogikh

Allocate the kcov buffer during KCOV_MODE_INIT in order to untie mmapping of a kcov instance and the actual coverage collection process. Modify kcov_mmap, so that it can be reliably used any number of times once KCOV_MODE_INIT has succeeded.

These changes to the user-facing interface of the tool only weaken the preconditions, so all existing user space code should remain compatible with the new version.

---
 kernel/kcov.c | 49 +++++++++++++++++++++++++------------------------
 1 file changed, 25 insertions(+), 24 deletions(-)
kernel/kcov.c +25 -24
index 5d87b4e0126f..d6a522fc6f36 100644
@@ -459,37 +459,28 @@ void kcov_task_exit(struct task_struct *t)
 static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
 {
 	int res = 0;
-	void *area;
 	struct kcov *kcov = vma->vm_file->private_data;
 	unsigned long size, off;
 	struct page *page;
 	unsigned long flags;
 
-	area = vmalloc_user(vma->vm_end - vma->vm_start);
-	if (!area)
-		return -ENOMEM;
-
 	spin_lock_irqsave(&kcov->lock, flags);
 	size = kcov->size * sizeof(unsigned long);
-	if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 ||
+	if (kcov->area == NULL || vma->vm_pgoff != 0 ||
 	    vma->vm_end - vma->vm_start != size) {
 		res = -EINVAL;
 		goto exit;
 	}
-	if (!kcov->area) {
-		kcov->area = area;
-		vma->vm_flags |= VM_DONTEXPAND;
-		spin_unlock_irqrestore(&kcov->lock, flags);
-		for (off = 0; off < size; off += PAGE_SIZE) {
-			page = vmalloc_to_page(kcov->area + off);
-			if (vm_insert_page(vma, vma->vm_start + off, page))
-				WARN_ONCE(1, "vm_insert_page() failed");
-		}
-		return 0;
+	spin_unlock_irqrestore(&kcov->lock, flags);
+	vma->vm_flags |= VM_DONTEXPAND;
+	for (off = 0; off < size; off += PAGE_SIZE) {
+		page = vmalloc_to_page(kcov->area + off);
+		if (vm_insert_page(vma, vma->vm_start + off, page))
+			WARN_ONCE(1, "vm_insert_page() failed");
 	}
+	return 0;
 exit:
 	spin_unlock_irqrestore(&kcov->lock, flags);
-	vfree(area);
 	return res;
 }
 
@@ -671,25 +662,35 @@ static int kcov_ioctl_unlocked(struct kcov *kcov, unsigned int cmd,
 {
 	unsigned long size, flags;
 	int res;
+	void *area;
 
 	switch (cmd) {
 	case KCOV_INIT_TRACE:
 		/*
 		 * Enable kcov in trace mode and setup buffer size.
 		 * Must happen before anything else.
-		 */
-		if (kcov->mode != KCOV_MODE_DISABLED)
-			return -EBUSY;
-		/*
-		 * Size must be at least 2 to hold current position and one PC.
-		 * Later we allocate size * sizeof(unsigned long) memory,
-		 * that must not overflow.
+		 *
+		 * First check the size argument - it must be at least 2
+		 * to hold the current position and one PC.
 		 */
 		size = arg;
 		if (size < 2 || size > INT_MAX / sizeof(unsigned long))
 			return -EINVAL;
+
+		area = vmalloc_user(size * sizeof(unsigned long));
+		if (area == NULL)
+			return -ENOMEM;
+
+		spin_lock_irqsave(&kcov->lock, flags);
+		if (kcov->mode != KCOV_MODE_DISABLED) {
+			spin_unlock_irqrestore(&kcov->lock, flags);
+			vfree(area);
+			return -EBUSY;
+		}
+		kcov->area = area;
 		kcov->size = size;
 		kcov->mode = KCOV_MODE_INIT;
+		spin_unlock_irqrestore(&kcov->lock, flags);
 		return 0;
 	default:
 		/*
Signature · 1 line
2.34.1.307.g9b7440fafd-goog