sisco311 / scrcpy-snap

Snap packaging for scrcpy: https://github.com/Genymobile/scrcpy
Apache License 2.0
53 stars 12 forks source link

scrcpy and scrcpy.adb have udev and apparmor rules incorrect in snap rev 18 #4

Closed fhteagle closed 5 years ago

fhteagle commented 5 years ago

Migrating this bug from scrcpy issues ( https://github.com/Genymobile/scrcpy/issues/464 ) to here:

System adb version is not used when scrcpy is installed via snap on Ubuntu 18.10. This was noticed on snap version 18, no other snap versions tested. System adb version is reported as 39, is capable of seeing USB attached phones, works as expected. Scrcpy.adb version is reported as 36, does not list USB connected phones (possibly due to udev rules), which makes scrcpy unusable over USB. Scrcpy and Scrcpy.adb via tcpip requires first setting tcpip mode on device with system adb.

Steps tried with no success:

  1. Uninstalling / reinstalling system adb via apt
  2. setting ADB environment variable in Ubuntu and in snap environment
  3. issuing the command $sudo snap connect scrcpy:adb-support :adb-support
Slot             Plug
:adb-support     scrcpy
:desktop         anbox,gnome-calculator,gnome-characters,gnome-logs,gnome-system-monitor,scrcpy
:desktop-legacy  gnome-calculator,gnome-characters,gnome-logs,gnome-system-monitor,scrcpy
:network         anbox,electrum,gnome-calculator,gnome-logs,scrcpy
:opengl          anbox,scrcpy
:pulseaudio      anbox,scrcpy
:unity7          anbox,gnome-calculator,gnome-characters,gnome-logs,gnome-system-monitor,scrcpy
:wayland         anbox,gnome-calculator,gnome-characters,gnome-logs,gnome-system-monitor,scrcpy
:x11             anbox,electrum,scrcpy
sisco311 commented 5 years ago

Snaps run under a restrictive security sandbox. The adb-support interface installs its own apparmor and udev rules. Please check if your phone's vendor id is listed in the udev rule file created by the interface in /etc/udev/rules.d/70-snap.scrcpy.rules

If not, then try to create your own udev rule, /etc/udev/rules.d/99-add-device-to-scrcpy.rules SUBSYSTEM=="usb", ATTR{idVendor}=="XXXX", TAG+="snap_scrcpy_adb" SUBSYSTEM=="usb", ATTR{idVendor}=="XXXX", TAG+="snap_scrcpy_scrcpy"

Where XXXX is the vendor id reported by lsusb

restart udev and kill any running adb server, then check if the builtin adb detects your device: sudo udevadm control --reload sudo udevadm trigger adb kill-server scrcpy.adb kill-server scrcpy.adb devices

Thanks, sisco311

fhteagle commented 5 years ago

@sisco311 Thanks for that, we are getting closer. After following your directions, the OnePlus and a second ZTE phone over USB each do show up in the output from scrcpy.adb devices. However, there is no serial number reported in the output of scrcpy.adb devices list for either phone. scrcpy.adb devices -l turns up accurate information (except for serial number of course). Unfortunately, specifying the known serial number of the device in scrcpy -s 4a....39 fails. See below:

$ scrcpy.adb devices
List of devices attached
(no serial number)      device
emulator-5..8   offline
$ scrcpy.adb devices -l
List of devices attached
emulator-5558          device product:anbox_x86_64 model:Anbox device:x86_64
(no serial number)     device usb:1-1.1 product:OnePlus6 model:ONEPLUS_A6003 device:OnePlus6
$ scrcpy.adb -s 4a....39 shell
error: device '4a....39' not found

However, if I manually kill the anbox emulator instance, so that the OnePlus or ZTE is the only device in scrcpy.adb devices, scrcpy works as expected.

As a future goal, I am trying to work towards a script that would automatically startup scrcpy on plug-in, but I cannot see a way to force scrcpy to select a phone instead of an emulator without a serial number. None of the flags that work in adb or scrcpy.adb such as -d work in straight scrcpy.

sisco311 commented 5 years ago

We have found a bug! The apparmor profile generated by the adb-support interface grants read access for /sys/devices/**/usb*/*/serial r, but the serial file on my system is one level deeper in the directory hierarchy.

If I change the relevant lines in /var/lib/snapd/apparmor/profiles/snap.scrcpy.scrcpy and /var/lib/snapd/apparmor/profiles/snap.scrcpy.adb to /sys/devices/**/usb*/**/serial r, and reload the profiles sudo apparmor_parser -r /var/lib/snapd/apparmor/profiles/snap.scrcpy.scrcpy sudo apparmor_parser -r /var/lib/snapd/apparmor/profiles/snap.scrcpy.adb everything works as expected.

fhteagle commented 5 years ago

@sisco311 Good find! I was able to duplicate your fix on my machine as well! Second command should probably be sudo apparmor_parser -r /var/lib/snapd/apparmor/profiles/snap.scrcpy.adb.

Changing bug title to reflect causes and symptoms.

Two things are still not quite working right, though:

  1. I have to issue /snap/bin/scrcpy.adb devices twice to get the OnePlus device to show up. Only the ZTE device shows up on the first time the command is executed. OnePlus always shows up on the second issuing of the command. Waiting arbitrary time between plug in and issuing the command seems to have no effect. Odd.
  2. Second, I am finding dmesg messages after my script fires from udev (and fails to start scrcpy, despite the script working from my user's terminal). The dmesg entry is similar to: `audit: type=1400 audit(1553362545.429:2780): apparmor="DENIED" operation="open" profile="snap.scrcpy.scrcpy" name="/media/home/pr....se/.cache/mesa_shader_cache/index" pid=21102 comm="scrcpy" requested_mask="wrc" denied_mask="wrc" fsuid=1000 ouid=1000 Permissions on this file are 644 when ls'ing .
sisco311 commented 5 years ago

Cool.

Reported the apparmor bug.

Please post the output of: lsusb --verbose -d xxxx: Where xxxx is the vendor id of the OnePlus

I will make a feature request upstream to include your device to the trusted ones.

One thing is still not quite working right, though. I have to issue /snap/bin/scrcpy.adb devices twice to get the OnePlus device to show up. Only the ZTE device shows up on the first time the command is executed. OnePlus always shows up on the second issuing of the command. Waiting arbitrary time between plug in and issuing the command seems to have no effect. Odd.

Please open a new issue for this I will look into it

fhteagle commented 5 years ago

Here is the lsusb output:

Bus 001 Device 045: ID 2a70:4ee7  
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x2a70 
  idProduct          0x4ee7 
  bcdDevice            4.09
  iManufacturer           1 OnePlus
  iProduct                2 OnePlus
  iSerial                 3 4a....39
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           32
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          4 adb
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              500mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass     66 
      bInterfaceProtocol      1 
      iInterface              5 ADB Interface
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength           12
  bNumDeviceCaps          1
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x00000006
      Link Power Management (LPM) Supported
Device Status:     0x0000
  (Bus Powered)
sisco311 commented 5 years ago

Closing this.

Feature request for including OnePlus @ https://bugs.launchpad.net/snapd/+bug/1821474

sisco311 commented 5 years ago

UPDATE:

Your devices will be supported in snapd 2.39: https://bugs.launchpad.net/snapd/+bug/1821474

The apparmor issue is addressed as well and will be fixed in 2.39: https://bugs.launchpad.net/snapd/+bug/1821474

sisco311 commented 5 years ago

One more thing. The OnePlus was added to the trusted devices: https://github.com/snapcore/snapd/pull/6645

But, as pointed out by zyga, your device is not listed at http://www.linux-usb.org/usb.ids

When you have a little time please add it the database: http://www.linux-usb.org/usb-ids.html

Thanks, sisco311

fhteagle commented 5 years ago

Odd thing is that that U:D pair appears in their web portal: https://usb-ids.gowdy.us/read/UD/2a70/4ee7 From the commentary, it looks like 2a70:4ee7 has been used by several models of OnePlus 6 phones when connecting in android debug mode. I emailed the curators of the list to see if the U:D pair can be added to the official usb.ids file after all.