Skip to content

Commit

Permalink
Ensure port still exists when trying to gain elevated access
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Jan 28, 2022
1 parent bc2ec3e commit 93011c9
Show file tree
Hide file tree
Showing 28 changed files with 35 additions and 50 deletions.
54 changes: 29 additions & 25 deletions src/main/c/Posix/PosixHelperFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1640,37 +1640,41 @@ int verifyAndSetUserPortGroup(const char *portFile)
// Attempt to acquire access if not available
if (!userCanAccess)
{
// Check if the user is part of the group that owns the port
// Ensure that the port still exists
struct stat fileStats;
gid_t *userGroups = (gid_t*)malloc(numGroups * sizeof(gid_t));
if ((stat(portFile, &fileStats) == 0) && (getgroups(numGroups, userGroups) >= 0))
for (int i = 0; i < numGroups; ++i)
if (userGroups[i] == fileStats.st_gid)
{
userPartOfPortGroup = 1;
break;
}

// Attempt to add the user to the group that owns the port
char *addUserToGroupCmd = (char*)malloc(256);
if (!userPartOfPortGroup)
if (stat(portFile, &fileStats) == 0)
{
struct group *portGroup;
struct passwd *userDetails;
if ((portGroup = getgrgid(fileStats.st_gid)) && (userDetails = getpwuid(geteuid())))
// Check if the user is part of the group that owns the port
gid_t *userGroups = (gid_t*)malloc(numGroups * sizeof(gid_t));
if (getgroups(numGroups, userGroups) >= 0)
for (int i = 0; i < numGroups; ++i)
if (userGroups[i] == fileStats.st_gid)
{
userPartOfPortGroup = 1;
break;
}

// Attempt to add the user to the group that owns the port
char *addUserToGroupCmd = (char*)malloc(256);
if (!userPartOfPortGroup)
{
snprintf(addUserToGroupCmd, 256, "sudo usermod -a -G %s %s", portGroup->gr_name, userDetails->pw_name);
userCanAccess = (system(addUserToGroupCmd) == 0);
struct group *portGroup;
struct passwd *userDetails;
if ((portGroup = getgrgid(fileStats.st_gid)) && (userDetails = getpwuid(geteuid())))
{
snprintf(addUserToGroupCmd, 256, "sudo usermod -a -G %s %s", portGroup->gr_name, userDetails->pw_name);
userCanAccess = (system(addUserToGroupCmd) == 0);
}
}
}

// Attempt to enable all read/write port permissions
snprintf(addUserToGroupCmd, 256, "sudo chmod 666 %s", portFile);
userCanAccess = (system(addUserToGroupCmd) == 0) || userCanAccess;
// Attempt to enable all read/write port permissions
snprintf(addUserToGroupCmd, 256, "sudo chmod 666 %s", portFile);
userCanAccess = (system(addUserToGroupCmd) == 0) || userCanAccess;

// Clean up memory
free(addUserToGroupCmd);
free(userGroups);
// Clean up memory
free(addUserToGroupCmd);
free(userGroups);
}
}

// Return whether the user can currently access the serial port
Expand Down
Binary file modified src/main/resources/Android/arm64-v8a/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/armeabi-v7a/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/x86_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/FreeBSD/arm64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/FreeBSD/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/FreeBSD/x86_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv5/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv6/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv6hf/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv7/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv7hf/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv8_32/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv8_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/ppc64le/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/x86_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/OSX/aarch64/libjSerialComm.jnilib
Binary file not shown.
Binary file modified src/main/resources/OSX/x86/libjSerialComm.jnilib
Binary file not shown.
Binary file modified src/main/resources/OSX/x86_64/libjSerialComm.jnilib
Binary file not shown.
Binary file modified src/main/resources/OpenBSD/amd64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/OpenBSD/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Solaris/sparcv8plus_32/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Solaris/sparcv9_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Solaris/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Solaris/x86_64/libjSerialComm.so
Binary file not shown.
31 changes: 6 additions & 25 deletions src/test/java/com/fazecast/jSerialComm/SerialPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,22 @@ public void run()
ubxPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_PARITY_ERROR | SerialPort.LISTENING_EVENT_DATA_WRITTEN | SerialPort.LISTENING_EVENT_BREAK_INTERRUPT |
SerialPort.LISTENING_EVENT_CARRIER_DETECT | SerialPort.LISTENING_EVENT_CTS | SerialPort.LISTENING_EVENT_DSR | SerialPort.LISTENING_EVENT_RING_INDICATOR |
SerialPort.LISTENING_EVENT_CARRIER_DETECT | SerialPort.LISTENING_EVENT_CTS | SerialPort.LISTENING_EVENT_DSR | SerialPort.LISTENING_EVENT_RING_INDICATOR | SerialPort.LISTENING_EVENT_PORT_DISCONNECTED |
SerialPort.LISTENING_EVENT_FRAMING_ERROR | SerialPort.LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
@Override
public void serialEvent(SerialPortEvent event)
{
System.out.println("Received event type: " + event.getEventType());
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
{
System.out.println("Received event type: LISTENING_EVENT_DATA_AVAILABLE");
byte[] buffer = new byte[event.getSerialPort().bytesAvailable()];
event.getSerialPort().readBytes(buffer, buffer.length);
System.out.println(" Reading " + buffer.length + " bytes");
}
else if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED)
System.out.println("Received event type: LISTENING_EVENT_PORT_DISCONNECTED");
else
System.out.println("Received event type: " + event.getEventType());
}
});
try { Thread.sleep(5000); } catch (Exception e) {}
Expand Down Expand Up @@ -311,29 +315,6 @@ public void serialEvent(SerialPortEvent event)
ubxPort.removeDataListener();
System.out.println("\nClosing " + ubxPort.getDescriptivePortName() + ": " + ubxPort.closePort());

/*System.out.println("\nPhysically unplug device within the next 10 seconds to see if the disconnect event fires...");
ubxPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED | SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
@Override
public void serialEvent(SerialPortEvent event)
{
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
{
System.out.println("Received event type: LISTENING_EVENT_DATA_AVAILABLE");
byte[] buffer = new byte[event.getSerialPort().bytesAvailable()];
event.getSerialPort().readBytes(buffer, buffer.length);
System.out.println(" Reading " + buffer.length + " bytes");
}
else
{
System.out.println("Received event type: LISTENING_EVENT_PORT_DISCONNECTED");
}
}
});
try { Thread.sleep(10000); } catch (Exception e) {}
ubxPort.closePort();*/

/*System.out.println("\n\nAttempting to read from two serial ports simultaneously\n");
System.out.println("\nAvailable Ports:\n");
for (int i = 0; i < ports.length; ++i)
Expand Down

0 comments on commit 93011c9

Please sign in to comment.