Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Calling startMonitoring() and stopMonitoring() multiple times causes segmentation fault (Linux, macOS) #57

Closed
qtphan734 opened this issue Apr 3, 2018 · 6 comments · Fixed by #162

Comments

@qtphan734
Copy link

qtphan734 commented Apr 3, 2018

In a simple node application, if you execute the folllowing code, you will either get segmentation fault or some memory corruption error on Ubuntu 16.04:

usbDetection.startMonitoring();
usbDetection.stopMonitoring();

usbDetection.startMonitoring();
usbDetection.stopMonitoring();
@qtphan734 qtphan734 changed the title Calling startMonitoring() and stopMonitoring() causes segmentation fault on Ubuntu 16.04 Calling startMonitoring() and stopMonitoring() multiple times causes segmentation fault on Ubuntu 16.04 Apr 3, 2018
@MadLittleMods
Copy link
Owner

Related to #53

@PeterBurner
Copy link

PeterBurner commented Sep 16, 2019

I don't think calling them multiple times is the problem. stopMonitoring() seems to be broken on linux.
I can generate a segmentation fault with the following code:

var usbDetect = require('usb-detection');

usbDetect.startMonitoring();

usbDetect.stopMonitoring();

setTimeout(() => console.log('end'), 1000);

If I comment out the line with stopMonitoring() I get a log output but afterwards the process hangs.

Node Version: v12.8.0
OS: Raspbian 10 (buster)

By the way: The code above works just fine on MacOS.

@P0oOOOo0YA
Copy link

I can reproduce the problem on my Linux machine with the code posted by @PeterBurner .

    OS: Linux 5.3 Kali GNU/Linux Rolling 2019.4
    Node: 13.5.0

@gre
Copy link

gre commented Jan 2, 2020

I reproduce this situation in [email protected] on Node 12 and Mac.

@frankrobert-ls
Copy link

As @gre mentioned this can be reproduced on Mac with Node12. Could we update this issue's title + labels to reflect it better? 🌈

@MadLittleMods MadLittleMods changed the title Calling startMonitoring() and stopMonitoring() multiple times causes segmentation fault on Ubuntu 16.04 Calling startMonitoring() and stopMonitoring() multiple times causes segmentation fault (Linux, macOS) Apr 24, 2020
@kfazz
Copy link

kfazz commented Jul 9, 2021

For 4.10, I was able to fix this with the following patch:

linux_detection_fix_udev_mon_use_after_free.txt

linux_detection_fix_udev_mon_use_after_free patch
Description: linux detection: fix udev mon use after free
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 node-usb-detection (4.10.0-2) UNRELEASED; urgency=medium
 .
   [ Ken Fazzone ]
   * Initial release
Author: Ken Fazzone <[email protected]>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: 2021-01-19

--- node-usb-detection-4.10.0.orig/src/detection_linux.cpp
+++ node-usb-detection-4.10.0/src/detection_linux.cpp
@@ -98,6 +98,7 @@ void Stop() {
 	uv_cond_destroy(&notifyDeviceHandled);
 
 	udev_monitor_unref(mon);
+	mon = NULL;
 	udev_unref(udev);
 }
 
@@ -226,19 +227,21 @@ static void cbWork(uv_work_t *req) {
 		if (!ret) continue;
 		if (ret < 0) break;
 
-		dev = udev_monitor_receive_device(mon);
-		if (dev) {
-			if(udev_device_get_devtype(dev) && strcmp(udev_device_get_devtype(dev), DEVICE_TYPE_DEVICE) == 0) {
-				if(strcmp(udev_device_get_action(dev), DEVICE_ACTION_ADDED) == 0) {
-					WaitForDeviceHandled();
-					DeviceAdded(dev);
-				}
-				else if(strcmp(udev_device_get_action(dev), DEVICE_ACTION_REMOVED) == 0) {
-					WaitForDeviceHandled();
-					DeviceRemoved(dev);
+		if(mon) {
+			dev = udev_monitor_receive_device(mon);
+			if (dev) {
+				if(udev_device_get_devtype(dev) && strcmp(udev_device_get_devtype(dev), DEVICE_TYPE_DEVICE) == 0) {
+					if(strcmp(udev_device_get_action(dev), DEVICE_ACTION_ADDED) == 0) {
+						WaitForDeviceHandled();
+						DeviceAdded(dev);
+					}
+					else if(strcmp(udev_device_get_action(dev), DEVICE_ACTION_REMOVED) == 0) {
+						WaitForDeviceHandled();
+						DeviceRemoved(dev);
+					}
 				}
+				udev_device_unref(dev);
 			}
-			udev_device_unref(dev);
 		}
 	}
 }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants