github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/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 // - ttyMSMX (Qualcomm msm7x serial devices) 77 // - ttyHSX (Qualcomm GENI based QTI serial cores) 78 var serialDeviceNodePattern = regexp.MustCompile("^/dev/tty(mxc|USB|ACM|AMA|XRUSB|S|O|SC|MSM|HS)[0-9]+$") 79 80 // Pattern that is considered valid for the udev symlink to the serial device, 81 // path attributes will be compared to this for validity when usb vid and pid 82 // are also specified 83 var serialUDevSymlinkPattern = regexp.MustCompile("^/dev/serial-port-[a-z0-9]+$") 84 85 // BeforePrepareSlot checks validity of the defined slot 86 func (iface *serialPortInterface) BeforePrepareSlot(slot *snap.SlotInfo) error { 87 // Check slot has a path attribute identify serial device 88 path, ok := slot.Attrs["path"].(string) 89 if !ok || path == "" { 90 return fmt.Errorf("serial-port slot must have a path attribute") 91 } 92 93 // XXX: this interface feeds the cleaned path into the regex and is 94 // left unchanged here for historical reasons. New interfaces (eg, 95 // like raw-volume) should instead use verifySlotPathAttribute() which 96 // performs additional verification. 97 path = filepath.Clean(path) 98 99 if iface.hasUsbAttrs(slot) { 100 // Must be path attribute where symlink will be placed and usb vendor and product identifiers 101 // Check the path attribute is in the allowable pattern 102 if !serialUDevSymlinkPattern.MatchString(path) { 103 return fmt.Errorf("serial-port path attribute specifies invalid symlink location") 104 } 105 106 usbVendor, vOk := slot.Attrs["usb-vendor"].(int64) 107 if !vOk { 108 return fmt.Errorf("serial-port slot failed to find usb-vendor attribute") 109 } 110 if (usbVendor < 0x1) || (usbVendor > 0xFFFF) { 111 return fmt.Errorf("serial-port usb-vendor attribute not valid: %d", usbVendor) 112 } 113 114 usbProduct, pOk := slot.Attrs["usb-product"].(int64) 115 if !pOk { 116 return fmt.Errorf("serial-port slot failed to find usb-product attribute") 117 } 118 if (usbProduct < 0x0) || (usbProduct > 0xFFFF) { 119 return fmt.Errorf("serial-port usb-product attribute not valid: %d", usbProduct) 120 } 121 122 usbInterfaceNumber, ok := slot.Attrs["usb-interface-number"].(int64) 123 if ok && (usbInterfaceNumber < 0 || usbInterfaceNumber >= UsbMaxInterfaces) { 124 return fmt.Errorf("serial-port usb-interface-number attribute cannot be negative or larger than %d", UsbMaxInterfaces-1) 125 } 126 } else { 127 // Just a path attribute - must be a valid usb device node 128 // Check the path attribute is in the allowable pattern 129 if !serialDeviceNodePattern.MatchString(path) { 130 return fmt.Errorf("serial-port path attribute must be a valid device node") 131 } 132 } 133 return nil 134 } 135 136 func (iface *serialPortInterface) UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error { 137 var usbVendor, usbProduct, usbInterfaceNumber int64 138 var path string 139 if err := slot.Attr("usb-vendor", &usbVendor); err != nil { 140 return nil 141 } 142 if err := slot.Attr("usb-product", &usbProduct); err != nil { 143 return nil 144 } 145 if err := slot.Attr("path", &path); err != nil || path == "" { 146 return nil 147 } 148 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 149 spec.AddSnippet(fmt.Sprintf(`# serial-port 150 IMPORT{builtin}="usb_id" 151 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/"))) 152 } else { 153 spec.AddSnippet(fmt.Sprintf(`# serial-port 154 IMPORT{builtin}="usb_id" 155 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", SYMLINK+="%s"`, usbVendor, usbProduct, strings.TrimPrefix(path, "/dev/"))) 156 } 157 return nil 158 } 159 160 func (iface *serialPortInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 161 if iface.hasUsbAttrs(slot) { 162 // This apparmor rule is an approximation of serialDeviceNodePattern 163 // (AARE is different than regex, so we must approximate). 164 // UDev tagging and device cgroups will restrict down to the specific device 165 spec.AddSnippet("/dev/tty[A-Z]*[0-9] rwk,") 166 return nil 167 } 168 169 // Path to fixed device node 170 var path string 171 if err := slot.Attr("path", &path); err != nil { 172 return nil 173 } 174 cleanedPath := filepath.Clean(path) 175 spec.AddSnippet(fmt.Sprintf("%s rwk,", cleanedPath)) 176 return nil 177 } 178 179 func (iface *serialPortInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 180 // For connected plugs, we use vendor and product ids if available, 181 // otherwise add the kernel device 182 hasOnlyPath := !iface.hasUsbAttrs(slot) 183 var usbVendor, usbProduct int64 184 var path string 185 if err := slot.Attr("usb-vendor", &usbVendor); err != nil && !hasOnlyPath { 186 return nil 187 } 188 if err := slot.Attr("usb-product", &usbProduct); err != nil && !hasOnlyPath { 189 return nil 190 } 191 if err := slot.Attr("path", &path); err != nil && hasOnlyPath { 192 return nil 193 } 194 195 if hasOnlyPath { 196 spec.TagDevice(fmt.Sprintf(`SUBSYSTEM=="tty", KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/"))) 197 } else { 198 var usbInterfaceNumber int64 199 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 200 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 201 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", ENV{ID_USB_INTERFACE_NUM}=="%02x"`, usbVendor, usbProduct, usbInterfaceNumber)) 202 } else { 203 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 204 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x"`, usbVendor, usbProduct)) 205 } 206 } 207 return nil 208 } 209 210 func (iface *serialPortInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool { 211 // allow what declarations allowed 212 return true 213 } 214 215 func (iface *serialPortInterface) HotplugDeviceDetected(di *hotplug.HotplugDeviceInfo) (*hotplug.ProposedSlot, error) { 216 bus, _ := di.Attribute("ID_BUS") 217 if di.Subsystem() != "tty" || bus != "usb" || !serialDeviceNodePattern.MatchString(di.DeviceName()) { 218 return nil, nil 219 } 220 221 slot := hotplug.ProposedSlot{ 222 Attrs: map[string]interface{}{ 223 "path": di.DeviceName(), 224 }, 225 } 226 if vendor, ok := di.Attribute("ID_VENDOR_ID"); ok { 227 slot.Attrs["usb-vendor"] = vendor 228 } 229 if product, ok := di.Attribute("ID_MODEL_ID"); ok { 230 slot.Attrs["usb-product"] = product 231 } 232 return &slot, nil 233 } 234 235 func slotDeviceAttrEqual(di *hotplug.HotplugDeviceInfo, devinfoAttribute string, slotAttributeValue int64) bool { 236 var attr string 237 var ok bool 238 if attr, ok = di.Attribute(devinfoAttribute); !ok { 239 return false 240 } 241 val, err := strconv.ParseInt(attr, 16, 64) 242 return err == nil && val == slotAttributeValue 243 } 244 245 func (iface *serialPortInterface) HandledByGadget(di *hotplug.HotplugDeviceInfo, slot *snap.SlotInfo) bool { 246 // if the slot has vendor, product and interface number set, check if they match 247 var usbVendor, usbProduct, usbInterfaceNumber int64 248 if err := slot.Attr("usb-vendor", &usbVendor); err == nil { 249 if !slotDeviceAttrEqual(di, "ID_VENDOR_ID", usbVendor) { 250 return false 251 } 252 if err := slot.Attr("usb-product", &usbProduct); err != nil { 253 return false 254 } 255 if !slotDeviceAttrEqual(di, "ID_MODEL_ID", usbProduct) { 256 return false 257 } 258 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 259 if !slotDeviceAttrEqual(di, "ID_USB_INTERFACE_NUM", usbInterfaceNumber) { 260 return false 261 } 262 } 263 return true 264 } 265 266 var path string 267 if err := slot.Attr("path", &path); err != nil { 268 return false 269 } 270 return di.DeviceName() == path 271 } 272 273 func (iface *serialPortInterface) hasUsbAttrs(attrs interfaces.Attrer) bool { 274 var v int64 275 if err := attrs.Attr("usb-vendor", &v); err == nil { 276 return true 277 } 278 if err := attrs.Attr("usb-product", &v); err == nil { 279 return true 280 } 281 if err := attrs.Attr("usb-interface-number", &v); err == nil { 282 return true 283 } 284 return false 285 } 286 287 func init() { 288 registerIface(&serialPortInterface{}) 289 }