forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidraw.go
209 lines (180 loc) · 6.18 KB
/
hidraw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package builtin
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/udev"
"github.com/snapcore/snapd/snap"
)
const hidrawSummary = `allows access to specific hidraw device`
const hidrawBaseDeclarationSlots = `
hidraw:
allow-installation:
slot-snap-type:
- core
- gadget
deny-auto-connection: true
`
// hidrawInterface is the type for hidraw interfaces.
type hidrawInterface struct{}
// Name of the hidraw interface.
func (iface *hidrawInterface) Name() string {
return "hidraw"
}
func (iface *hidrawInterface) StaticInfo() interfaces.StaticInfo {
return interfaces.StaticInfo{
Summary: hidrawSummary,
BaseDeclarationSlots: hidrawBaseDeclarationSlots,
}
}
func (iface *hidrawInterface) String() string {
return iface.Name()
}
// Pattern to match allowed hidraw device nodes, path attributes will be
// compared to this for validity when not using udev identification
var hidrawDeviceNodePattern = regexp.MustCompile("^/dev/hidraw[0-9]{1,3}$")
// Pattern that is considered valid for the udev symlink to the hidraw device,
// path attributes will be compared to this for validity when usb vid and pid
// are also specified
var hidrawUDevSymlinkPattern = regexp.MustCompile("^/dev/hidraw-[a-z0-9]+$")
// BeforePrepareSlot checks validity of the defined slot
func (iface *hidrawInterface) BeforePrepareSlot(slot *snap.SlotInfo) error {
// Check slot has a path attribute identify hidraw device
path, ok := slot.Attrs["path"].(string)
if !ok || path == "" {
return fmt.Errorf("hidraw slots must have a path attribute")
}
// XXX: this interface feeds the cleaned path into the regex and is
// left unchanged here for historical reasons. New interfaces (eg,
// like raw-volume) should instead use verifySlotPathAttribute() which
// performs additional verification.
path = filepath.Clean(path)
if iface.hasUsbAttrs(slot) {
// Must be path attribute where symlink will be placed and usb vendor and product identifiers
// Check the path attribute is in the allowable pattern
if !hidrawUDevSymlinkPattern.MatchString(path) {
return fmt.Errorf("hidraw path attribute specifies invalid symlink location")
}
usbVendor, vOk := slot.Attrs["usb-vendor"].(int64)
if !vOk {
return fmt.Errorf("hidraw slot failed to find usb-vendor attribute")
}
if (usbVendor < 0x1) || (usbVendor > 0xFFFF) {
return fmt.Errorf("hidraw usb-vendor attribute not valid: %d", usbVendor)
}
usbProduct, pOk := slot.Attrs["usb-product"].(int64)
if !pOk {
return fmt.Errorf("hidraw slot failed to find usb-product attribute")
}
if (usbProduct < 0x0) || (usbProduct > 0xFFFF) {
return fmt.Errorf("hidraw usb-product attribute not valid: %d", usbProduct)
}
} else {
// Just a path attribute - must be a valid usb device node
// Check the path attribute is in the allowable pattern
if !hidrawDeviceNodePattern.MatchString(path) {
return fmt.Errorf("hidraw path attribute must be a valid device node")
}
}
return nil
}
func (iface *hidrawInterface) UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error {
usbVendor, ok := slot.Attrs["usb-vendor"].(int64)
if !ok {
return nil
}
usbProduct, ok := slot.Attrs["usb-product"].(int64)
if !ok {
return nil
}
path, ok := slot.Attrs["path"].(string)
if !ok || path == "" {
return nil
}
spec.AddSnippet(fmt.Sprintf(`# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", SYMLINK+="%s"`,
usbVendor, usbProduct, strings.TrimPrefix(path, "/dev/")))
return nil
}
func (iface *hidrawInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
if iface.hasUsbAttrs(slot) {
// This apparmor rule must match hidrawDeviceNodePattern
// UDev tagging and device cgroups will restrict down to the specific device
spec.AddSnippet("/dev/hidraw[0-9]{,[0-9],[0-9][0-9]} rw,")
return nil
}
// Path to fixed device node
var path string
if err := slot.Attr("path", &path); err != nil {
return err
}
cleanedPath := filepath.Clean(path)
spec.AddSnippet(fmt.Sprintf("%s rw,", cleanedPath))
return nil
}
func (iface *hidrawInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
hasOnlyPath := true
if iface.hasUsbAttrs(slot) {
hasOnlyPath = false
}
var usbVendor int64
var usbProduct int64
var path string
err := slot.Attr("usb-vendor", &usbVendor)
if err != nil && !hasOnlyPath {
return nil
}
err = slot.Attr("usb-product", &usbProduct)
if err != nil && !hasOnlyPath {
return nil
}
err = slot.Attr("path", &path)
if err != nil && hasOnlyPath {
return nil
}
if hasOnlyPath {
spec.TagDevice(fmt.Sprintf(`SUBSYSTEM=="hidraw", KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/")))
} else {
spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x"`, usbVendor, usbProduct))
}
return nil
}
func (iface *hidrawInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool {
// allow what declarations allowed
return true
}
func (iface *hidrawInterface) hasUsbAttrs(attrs interfaces.Attrer) bool {
var v int64
if err := attrs.Attr("usb-vendor", &v); err == nil {
return true
}
if err := attrs.Attr("usb-product", &v); err == nil {
return true
}
return false
}
func init() {
registerIface(&hidrawInterface{})
}