{"id":3329,"date":"2018-08-13T08:59:28","date_gmt":"2018-08-13T13:59:28","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=3329"},"modified":"2018-08-14T15:30:53","modified_gmt":"2018-08-14T20:30:53","slug":"running-openhab2-as-non-root-user-with-usb","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=3329","title":{"rendered":"Running OpenHAB2 As Non-Root User &#8212; With USB"},"content":{"rendered":"<p>I&#8217;ll prefix this saga with the fact <em>my<\/em> sad story is implementation specific (i.e. relevant to those using Fedora, RHEL, or CentOS). I know Ubuntu has its own history with handling locks, and I&#8217;m sure other distros do as well. <em>But<\/em> I don&#8217;t know the history there, nor do I know how they currently manage locking.<\/p>\n<p>We switched our openHAB installation to use a systemd unit file to run as a service and changed the execution to a non-root user. Since we knew the openhab service account needed to be a member of dialout and tty, and we&#8217;d set the account up properly, we expected everything would work beautifully.<\/p>\n<p>Aaaand &#8230; neither ZWave for ZigBee came online. Not because it couldn&#8217;t access the USB devices, but because the non-root user could not <em>lock<\/em> the USB devices. From journalctl, we see LOTS of error messages that are not reflected in openHAB:<\/p>\n<pre>-- Logs begin at Sun 2017-04-30 14:28:12 EDT, end at Sun 2018-08-12 19:10:32 EDT. --\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyUSB-55: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: [34B blob data]\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyUSB-5: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: [34B blob data]\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyUSB1: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: [34B blob data]\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyUSB0: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: [34B blob data]\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS31: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: [34B blob data]\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS30: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS29: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS28: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS27: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS26: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS25: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock fi&gt;\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: RXTX fhs_lock() Error: opening lock file: \/var\/lock\/LCK..ttyS24: Permission denied. FAILED TO OPEN: No such file or directory\r\nAug 12 18:36:19 server.domain.ccTLD start.sh[7448]: testRead() Lock file failed<\/pre>\n<p>And now my old-school Linux\/Unix knowledge totally screws me over &#8212; I <em>expected <\/em>a uucp group with write access to \/run\/lock. Except &#8230; there&#8217;s no such group. Evidently in RHEL 7.2, they started using a group named lock with permission to \/var\/lock to differentiate between serial devices (owned by uucp) and lock files. Nice bit of history, that, but Fedora and RedHat don&#8217;t do that anymore <em>either<\/em>.<\/p>\n<p>Having a group with write permission was deemed a <a href=\"https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=581884\" target=\"_blank\" rel=\"noopener\">latent privilege escalation vulnerability<\/a>, and they played around with having a lockdev binary writing files to \/run\/lock\/lockdev, the creation and configuration of lockdev was <a href=\"https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=692714\" target=\"_blank\" rel=\"noopener\">moved into systemd<\/a>, and then <a href=\"https:\/\/github.com\/systemd\/systemd\/blob\/08644b22bb40f946d53f953bea6d16f3d4c7c21b\/NEWS#L262-L267\" target=\"_blank\" rel=\"noopener\">removed from systemd in favor of approaches<\/a> [flock(), for instance].<\/p>\n<p>RXTX has a <a href=\"https:\/\/github.com\/NeuronRobotics\/nrjavaserial\/blob\/master\/src\/main\/c\/include\/SerialImp.h\" target=\"_blank\" rel=\"noopener\">hard-coded path<\/a> based on OS version &#8212; that is what is used to create the lock file. And as the \/run\/lock folder is writable only by the owner, root &#8230; that is what is failing.<\/p>\n<pre>#if defined(__linux__)\r\n\/*\r\n\tThis is a small hack to get mark and space parity working on older systems\r\n\thttps:\/\/bugzilla.redhat.com\/bugzilla\/show_bug.cgi?id=147533\r\n*\/\r\n#\tif !defined(CMSPAR)\r\n#\t\tdefine CMSPAR 010000000000\r\n#\tendif \/* CMSPAR *\/\r\n#\t\r\n#\tdefine DEVICEDIR \"\/dev\/\"\r\n#\tdefine LOCKDIR \"\/var\/lock\"\r\n#\tdefine LOCKFILEPREFIX \"LCK..\"\r\n#\tdefine FHS\r\n#endif \/* __linux__ *\/<\/pre>\n<p>Which is <em>odd<\/em> because I see a few threads about how nrjavaserial has been updated and as soon as the newer nrjavaserial gets bundled into the application, locking will all be sorted. And there&#8217;s <a href=\"https:\/\/github.com\/NeuronRobotics\/nrjavaserial\/issues\/60\" target=\"_blank\" rel=\"noopener\">an open issue<\/a> for exactly the problem we are having &#8230; which explains why I&#8217;m not seeing something <em>different<\/em> in their source code. Digging around more, it looks like they didn&#8217;t actually change the hardcoded paths but rather added support for liblockdev. Which prompted my hypothesis that simply installing the lockdev package would magically sort the issue. It did not.<\/p>\n<p>In the interim, though, we can just add write permission for \/run\/lock thorough the config file \/usr\/lib\/tmpfiles.d\/legacy.conf &#8212; the distro creates the lock directory owned by root:root. Original config lines:<\/p>\n<pre>d \/run\/lock 0755 root root -\r\nL \/var\/lock - - - - ..\/run\/lock<\/pre>\n<p>We can create the folder as owned by the lock group group and add group write permissions (realizing that creates the potential for privilege escalation attacks). Updated config lines:<\/p>\n<pre>#d \/run\/lock 0755 root root -\r\nd \/run\/lock 0775 root lock -\r\nL \/var\/lock - - - - ..\/run\/lock<\/pre>\n<p>Adding the openhab account to the lock group allows the LCK.. files to be created.<\/p>\n<pre>[lisa@server run]# usermod -a -G lock openhab\r\n[lisa@server run]# id openhab\r\nuid=964(openhab) gid=963(openhab) groups=963(openhab),5(tty),18(dialout),54(lock)<\/pre>\n<p>Either reboot to reprocess legacy.conf or manually change the ownership &amp; permissions on \/run\/lock. Either way, confirm that the changes are successful.<\/p>\n<pre>[lisa@server run]# chown root:lock \/run\/lock\r\n[lisa@server run]# chmod g+w lock\r\n[lisa@server lock]# ll \/run | grep lock\r\ndrwxrwxr-x  7 root           lock             200 Aug 13 14:03 lock\r\n\r\n<\/pre>\n<p>If you manually set the permissions, restart openHAB. Our devices are online, and we have lock files:<\/p>\n<pre>[lisa@seerver lock]# ll\r\ntotal 12\r\n-rw-r--r-- 1 root root 22 Aug 10 15:35 asound.state.lock\r\ndrwx------ 2 root root 60 Aug 10 15:30 iscsi\r\n-rw-r--r-- 1 openhab openhab 11 Aug 13 14:03 LCK..ttyUSB-5\r\n-rw-r--r-- 1 openhab openhab 11 Aug 13 14:03 LCK..ttyUSB-55\r\ndrwxrwxr-x 2 root lock 40 Aug 10 15:30 lockdev\r\ndrwx------ 2 root root 40 Aug 10 15:30 lvm\r\ndrwxr-xr-x 2 root root 40 Aug 10 15:30 ppp\r\ndrwxr-xr-x 2 root root 40 Aug 10 15:30 subsys<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll prefix this saga with the fact my sad story is implementation specific (i.e. relevant to those using Fedora, RHEL, or CentOS). I know Ubuntu has its own history with handling locks, and I&#8217;m sure other distros do as well. But I don&#8217;t know the history there, nor do I know how they currently manage &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63,30],"tags":[638,44,394,637],"class_list":["post-3329","post","type-post","status-publish","format-standard","hentry","category-home-automation","category-system-administration","tag-lockdev","tag-openhab","tag-openhab2","tag-rxtx"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3329"}],"version-history":[{"count":5,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3329\/revisions"}],"predecessor-version":[{"id":3338,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3329\/revisions\/3338"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}