github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/serial_port.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package builtin 21 22 import ( 23 "fmt" 24 "path/filepath" 25 "regexp" 26 "strconv" 27 "strings" 28 29 "github.com/snapcore/snapd/interfaces" 30 "github.com/snapcore/snapd/interfaces/apparmor" 31 "github.com/snapcore/snapd/interfaces/hotplug" 32 "github.com/snapcore/snapd/interfaces/udev" 33 "github.com/snapcore/snapd/snap" 34 ) 35 36 const serialPortSummary = `allows accessing a specific serial port` 37 38 const serialPortBaseDeclarationSlots = ` 39 serial-port: 40 allow-installation: 41 slot-snap-type: 42 - core 43 - gadget 44 deny-auto-connection: true 45 ` 46 47 // serialPortInterface is the type for serial port interfaces. 48 type serialPortInterface struct{} 49 50 // Name of the serial-port interface. 51 func (iface *serialPortInterface) Name() string { 52 return "serial-port" 53 } 54 55 func (iface *serialPortInterface) StaticInfo() interfaces.StaticInfo { 56 return interfaces.StaticInfo{ 57 Summary: serialPortSummary, 58 BaseDeclarationSlots: serialPortBaseDeclarationSlots, 59 } 60 } 61 62 func (iface *serialPortInterface) String() string { 63 return iface.Name() 64 } 65 66 // Pattern to match allowed serial device nodes, path attributes will be 67 // compared to this for validity when not using udev identification 68 // Known device node patterns we need to support 69 // - ttyUSBX (UART over USB devices) 70 // - ttyACMX (ACM modem devices ) 71 // - ttyXRUSBx (Exar Corp. USB UART devices) 72 // - ttySX (UART serial ports) 73 // - ttyOX (UART serial ports on ARM) 74 // - ttymxcX (serial ports on i.mx6UL) 75 // - ttySCX (NXP SC16IS7xx serial devices) 76 var serialDeviceNodePattern = regexp.MustCompile("^/dev/tty(mxc|USB|ACM|AMA|XRUSB|S|O|SC)[0-9]+$") 77 78 // Pattern that is considered valid for the udev symlink to the serial device, 79 // path attributes will be compared to this for validity when usb vid and pid 80 // are also specified 81 var serialUDevSymlinkPattern = regexp.MustCompile("^/dev/serial-port-[a-z0-9]+$") 82 83 // BeforePrepareSlot checks validity of the defined slot 84 func (iface *serialPortInterface) BeforePrepareSlot(slot *snap.SlotInfo) error { 85 // Check slot has a path attribute identify serial device 86 path, ok := slot.Attrs["path"].(string) 87 if !ok || path == "" { 88 return fmt.Errorf("serial-port slot must have a path attribute") 89 } 90 91 // XXX: this interface feeds the cleaned path into the regex and is 92 // left unchanged here for historical reasons. New interfaces (eg, 93 // like raw-volume) should instead use verifySlotPathAttribute() which 94 // performs additional verification. 95 path = filepath.Clean(path) 96 97 if iface.hasUsbAttrs(slot) { 98 // Must be path attribute where symlink will be placed and usb vendor and product identifiers 99 // Check the path attribute is in the allowable pattern 100 if !serialUDevSymlinkPattern.MatchString(path) { 101 return fmt.Errorf("serial-port path attribute specifies invalid symlink location") 102 } 103 104 usbVendor, vOk := slot.Attrs["usb-vendor"].(int64) 105 if !vOk { 106 return fmt.Errorf("serial-port slot failed to find usb-vendor attribute") 107 } 108 if (usbVendor < 0x1) || (usbVendor > 0xFFFF) { 109 return fmt.Errorf("serial-port usb-vendor attribute not valid: %d", usbVendor) 110 } 111 112 usbProduct, pOk := slot.Attrs["usb-product"].(int64) 113 if !pOk { 114 return fmt.Errorf("serial-port slot failed to find usb-product attribute") 115 } 116 if (usbProduct < 0x0) || (usbProduct > 0xFFFF) { 117 return fmt.Errorf("serial-port usb-product attribute not valid: %d", usbProduct) 118 } 119 120 usbInterfaceNumber, ok := slot.Attrs["usb-interface-number"].(int64) 121 if ok && (usbInterfaceNumber < 0 || usbInterfaceNumber >= UsbMaxInterfaces) { 122 return fmt.Errorf("serial-port usb-interface-number attribute cannot be negative or larger than %d", UsbMaxInterfaces-1) 123 } 124 } else { 125 // Just a path attribute - must be a valid usb device node 126 // Check the path attribute is in the allowable pattern 127 if !serialDeviceNodePattern.MatchString(path) { 128 return fmt.Errorf("serial-port path attribute must be a valid device node") 129 } 130 } 131 return nil 132 } 133 134 func (iface *serialPortInterface) UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error { 135 var usbVendor, usbProduct, usbInterfaceNumber int64 136 var path string 137 if err := slot.Attr("usb-vendor", &usbVendor); err != nil { 138 return nil 139 } 140 if err := slot.Attr("usb-product", &usbProduct); err != nil { 141 return nil 142 } 143 if err := slot.Attr("path", &path); err != nil || path == "" { 144 return nil 145 } 146 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 147 spec.AddSnippet(fmt.Sprintf(`# serial-port 148 IMPORT{builtin}="usb_id" 149 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", ENV{ID_USB_INTERFACE_NUM}=="%02x", SYMLINK+="%s"`, usbVendor, usbProduct, usbInterfaceNumber, strings.TrimPrefix(path, "/dev/"))) 150 } else { 151 spec.AddSnippet(fmt.Sprintf(`# serial-port 152 IMPORT{builtin}="usb_id" 153 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", SYMLINK+="%s"`, usbVendor, usbProduct, strings.TrimPrefix(path, "/dev/"))) 154 } 155 return nil 156 } 157 158 func (iface *serialPortInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 159 if iface.hasUsbAttrs(slot) { 160 // This apparmor rule is an approximation of serialDeviceNodePattern 161 // (AARE is different than regex, so we must approximate). 162 // UDev tagging and device cgroups will restrict down to the specific device 163 spec.AddSnippet("/dev/tty[A-Z]*[0-9] rwk,") 164 return nil 165 } 166 167 // Path to fixed device node 168 var path string 169 if err := slot.Attr("path", &path); err != nil { 170 return nil 171 } 172 cleanedPath := filepath.Clean(path) 173 spec.AddSnippet(fmt.Sprintf("%s rwk,", cleanedPath)) 174 return nil 175 } 176 177 func (iface *serialPortInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 178 // For connected plugs, we use vendor and product ids if available, 179 // otherwise add the kernel device 180 hasOnlyPath := !iface.hasUsbAttrs(slot) 181 var usbVendor, usbProduct int64 182 var path string 183 if err := slot.Attr("usb-vendor", &usbVendor); err != nil && !hasOnlyPath { 184 return nil 185 } 186 if err := slot.Attr("usb-product", &usbProduct); err != nil && !hasOnlyPath { 187 return nil 188 } 189 if err := slot.Attr("path", &path); err != nil && hasOnlyPath { 190 return nil 191 } 192 193 if hasOnlyPath { 194 spec.TagDevice(fmt.Sprintf(`SUBSYSTEM=="tty", KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/"))) 195 } else { 196 var usbInterfaceNumber int64 197 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 198 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 199 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", ENV{ID_USB_INTERFACE_NUM}=="%02x"`, usbVendor, usbProduct, usbInterfaceNumber)) 200 } else { 201 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 202 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x"`, usbVendor, usbProduct)) 203 } 204 } 205 return nil 206 } 207 208 func (iface *serialPortInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool { 209 // allow what declarations allowed 210 return true 211 } 212 213 func (iface *serialPortInterface) HotplugDeviceDetected(di *hotplug.HotplugDeviceInfo) (*hotplug.ProposedSlot, error) { 214 bus, _ := di.Attribute("ID_BUS") 215 if di.Subsystem() != "tty" || bus != "usb" || !serialDeviceNodePattern.MatchString(di.DeviceName()) { 216 return nil, nil 217 } 218 219 slot := hotplug.ProposedSlot{ 220 Attrs: map[string]interface{}{ 221 "path": di.DeviceName(), 222 }, 223 } 224 if vendor, ok := di.Attribute("ID_VENDOR_ID"); ok { 225 slot.Attrs["usb-vendor"] = vendor 226 } 227 if product, ok := di.Attribute("ID_MODEL_ID"); ok { 228 slot.Attrs["usb-product"] = product 229 } 230 return &slot, nil 231 } 232 233 func slotDeviceAttrEqual(di *hotplug.HotplugDeviceInfo, devinfoAttribute string, slotAttributeValue int64) bool { 234 var attr string 235 var ok bool 236 if attr, ok = di.Attribute(devinfoAttribute); !ok { 237 return false 238 } 239 val, err := strconv.ParseInt(attr, 16, 64) 240 return err == nil && val == slotAttributeValue 241 } 242 243 func (iface *serialPortInterface) HandledByGadget(di *hotplug.HotplugDeviceInfo, slot *snap.SlotInfo) bool { 244 // if the slot has vendor, product and interface number set, check if they match 245 var usbVendor, usbProduct, usbInterfaceNumber int64 246 if err := slot.Attr("usb-vendor", &usbVendor); err == nil { 247 if !slotDeviceAttrEqual(di, "ID_VENDOR_ID", usbVendor) { 248 return false 249 } 250 if err := slot.Attr("usb-product", &usbProduct); err != nil { 251 return false 252 } 253 if !slotDeviceAttrEqual(di, "ID_MODEL_ID", usbProduct) { 254 return false 255 } 256 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 257 if !slotDeviceAttrEqual(di, "ID_USB_INTERFACE_NUM", usbInterfaceNumber) { 258 return false 259 } 260 } 261 return true 262 } 263 264 var path string 265 if err := slot.Attr("path", &path); err != nil { 266 return false 267 } 268 return di.DeviceName() == path 269 } 270 271 func (iface *serialPortInterface) hasUsbAttrs(attrs interfaces.Attrer) bool { 272 var v int64 273 if err := attrs.Attr("usb-vendor", &v); err == nil { 274 return true 275 } 276 if err := attrs.Attr("usb-product", &v); err == nil { 277 return true 278 } 279 if err := attrs.Attr("usb-interface-number", &v); err == nil { 280 return true 281 } 282 return false 283 } 284 285 func init() { 286 registerIface(&serialPortInterface{}) 287 }