/
cmd_run.go
420 lines (359 loc) · 12.3 KB
/
cmd_run.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2016 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 main
import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapenv"
"github.com/snapcore/snapd/x11"
)
var (
syscallExec = syscall.Exec
userCurrent = user.Current
osGetenv = os.Getenv
)
type cmdRun struct {
Command string `long:"command" hidden:"yes"`
Hook string `long:"hook" hidden:"yes"`
Revision string `short:"r" default:"unset" hidden:"yes"`
Shell bool `long:"shell" `
}
func init() {
addCommand("run",
i18n.G("Run the given snap command"),
i18n.G("Run the given snap command with the right confinement and environment"),
func() flags.Commander {
return &cmdRun{}
}, map[string]string{
"command": i18n.G("Alternative command to run"),
"hook": i18n.G("Hook to run"),
"r": i18n.G("Use a specific snap revision when running hook"),
"shell": i18n.G("Run a shell instead of the command (useful for debugging)"),
}, nil)
}
func (x *cmdRun) Execute(args []string) error {
if len(args) == 0 {
return fmt.Errorf(i18n.G("need the application to run as argument"))
}
snapApp := args[0]
args = args[1:]
// Catch some invalid parameter combinations, provide helpful errors
if x.Hook != "" && x.Command != "" {
return fmt.Errorf(i18n.G("cannot use --hook and --command together"))
}
if x.Revision != "unset" && x.Revision != "" && x.Hook == "" {
return fmt.Errorf(i18n.G("-r can only be used with --hook"))
}
if x.Hook != "" && len(args) > 0 {
// TRANSLATORS: %q is the hook name; %s a space-separated list of extra arguments
return fmt.Errorf(i18n.G("too many arguments for hook %q: %s"), x.Hook, strings.Join(args, " "))
}
// Now actually handle the dispatching
if x.Hook != "" {
return snapRunHook(snapApp, x.Revision, x.Hook)
}
// pass shell as a special command to snap-exec
if x.Shell {
x.Command = "shell"
}
return snapRunApp(snapApp, x.Command, args)
}
func getSnapInfo(snapName string, revision snap.Revision) (*snap.Info, error) {
if revision.Unset() {
curFn := filepath.Join(dirs.SnapMountDir, snapName, "current")
realFn, err := os.Readlink(curFn)
if err != nil {
return nil, fmt.Errorf("cannot find current revision for snap %s: %s", snapName, err)
}
rev := filepath.Base(realFn)
revision, err = snap.ParseRevision(rev)
if err != nil {
return nil, fmt.Errorf("cannot read revision %s: %s", rev, err)
}
}
info, err := snap.ReadInfo(snapName, &snap.SideInfo{
Revision: revision,
})
if err != nil {
return nil, err
}
return info, nil
}
func createOrUpdateUserDataSymlink(info *snap.Info, usr *user.User) error {
// 'current' symlink for user data (SNAP_USER_DATA)
userData := info.UserDataDir(usr.HomeDir)
wantedSymlinkValue := filepath.Base(userData)
currentActiveSymlink := filepath.Join(userData, "..", "current")
var err error
var currentSymlinkValue string
for i := 0; i < 5; i++ {
currentSymlinkValue, err = os.Readlink(currentActiveSymlink)
// Failure other than non-existing symlink is fatal
if err != nil && !os.IsNotExist(err) {
// TRANSLATORS: %v the error message
return fmt.Errorf(i18n.G("cannot read symlink: %v"), err)
}
if currentSymlinkValue == wantedSymlinkValue {
break
}
if err == nil {
// We may be racing with other instances of snap-run that try to do the same thing
// If the symlink is already removed then we can ignore this error.
err = os.Remove(currentActiveSymlink)
if err != nil && !os.IsNotExist(err) {
// abort with error
break
}
}
err = os.Symlink(wantedSymlinkValue, currentActiveSymlink)
// Error other than symlink already exists will abort and be propagated
if err == nil || !os.IsExist(err) {
break
}
// If we arrived here it means the symlink couldn't be created because it got created
// in the meantime by another instance, so we will try again.
}
if err != nil {
return fmt.Errorf(i18n.G("cannot update the 'current' symlink of %q: %v"), currentActiveSymlink, err)
}
return nil
}
func createUserDataDirs(info *snap.Info) error {
usr, err := userCurrent()
if err != nil {
return fmt.Errorf(i18n.G("cannot get the current user: %v"), err)
}
// see snapenv.User
userData := info.UserDataDir(usr.HomeDir)
commonUserData := info.UserCommonDataDir(usr.HomeDir)
for _, d := range []string{userData, commonUserData} {
if err := os.MkdirAll(d, 0755); err != nil {
// TRANSLATORS: %q is the directory whose creation failed, %v the error message
return fmt.Errorf(i18n.G("cannot create %q: %v"), d, err)
}
}
return createOrUpdateUserDataSymlink(info, usr)
}
func snapRunApp(snapApp, command string, args []string) error {
snapName, appName := snap.SplitSnapApp(snapApp)
info, err := getSnapInfo(snapName, snap.R(0))
if err != nil {
return err
}
app := info.Apps[appName]
if app == nil {
return fmt.Errorf(i18n.G("cannot find app %q in %q"), appName, snapName)
}
return runSnapConfine(info, app.SecurityTag(), snapApp, command, "", args)
}
func snapRunHook(snapName, snapRevision, hookName string) error {
revision, err := snap.ParseRevision(snapRevision)
if err != nil {
return err
}
info, err := getSnapInfo(snapName, revision)
if err != nil {
return err
}
hook := info.Hooks[hookName]
if hook == nil {
return fmt.Errorf(i18n.G("cannot find hook %q in %q"), hookName, snapName)
}
return runSnapConfine(info, hook.SecurityTag(), snapName, "", hook.Name, nil)
}
var osReadlink = os.Readlink
func isReexeced() bool {
exe, err := osReadlink("/proc/self/exe")
if err != nil {
logger.Noticef("cannot read /proc/self/exe: %v", err)
return false
}
return strings.HasPrefix(exe, dirs.SnapMountDir)
}
func migrateXauthority(info *snap.Info) (string, error) {
u, err := userCurrent()
if err != nil {
return "", fmt.Errorf(i18n.G("cannot get the current user: %s"), err)
}
// If our target directory (XDG_RUNTIME_DIR) doesn't exist we
// don't attempt to create it.
baseTargetDir := filepath.Join(dirs.XdgRuntimeDirBase, u.Uid)
if !osutil.FileExists(baseTargetDir) {
return "", nil
}
xauthPath := osGetenv("XAUTHORITY")
if len(xauthPath) == 0 || !osutil.FileExists(xauthPath) {
// Nothing to do for us. Most likely running outside of any
// graphical X11 session.
return "", nil
}
fin, err := os.Open(xauthPath)
if err != nil {
return "", err
}
defer fin.Close()
// Abs() also calls Clean(); see https://golang.org/pkg/path/filepath/#Abs
xauthPathAbs, err := filepath.Abs(fin.Name())
if err != nil {
return "", nil
}
// Remove all symlinks from path
xauthPathCan, err := filepath.EvalSymlinks(xauthPathAbs)
if err != nil {
return "", nil
}
// Ensure the XAUTHORITY env is not abused by checking that
// it point to exactly the file we just opened (no symlinks,
// no funny "../.." etc)
if fin.Name() != xauthPathCan {
logger.Noticef("WARNING: XAUTHORITY environment value is not a clean path: %q", xauthPathCan)
return "", nil
}
// Only do the migration from /tmp since the real /tmp is not visible for snaps
if !strings.HasPrefix(fin.Name(), "/tmp/") {
return "", nil
}
// We are performing a Stat() here to make sure that the user can't
// steal another user's Xauthority file. Note that while Stat() uses
// fstat() on the file descriptor created during Open(), the file might
// have changed ownership between the Open() and the Stat(). That's ok
// because we aren't trying to block access that the user already has:
// if the user has the privileges to chown another user's Xauthority
// file, we won't block that since the user can just steal it without
// having to use snap run. This code is just to ensure that a user who
// doesn't have those privileges can't steal the file via snap run
// (also note that the (potentially untrusted) snap isn't running yet).
fi, err := fin.Stat()
if err != nil {
return "", err
}
sys := fi.Sys()
if sys == nil {
return "", fmt.Errorf(i18n.G("cannot validate owner of file %s"), fin.Name())
}
// cheap comparison as the current uid is only available as a string
// but it is better to convert the uid from the stat result to a
// string than a string into a number.
if fmt.Sprintf("%d", sys.(*syscall.Stat_t).Uid) != u.Uid {
return "", fmt.Errorf(i18n.G("Xauthority file isn't owned by the current user %s"), u.Uid)
}
targetPath := filepath.Join(baseTargetDir, ".Xauthority")
// Only validate Xauthority file again when both files don't match
// otherwise we can continue using the existing Xauthority file.
// This is ok to do here because we aren't trying to protect against
// the user changing the Xauthority file in XDG_RUNTIME_DIR outside
// of snapd.
if osutil.FileExists(targetPath) {
var fout *os.File
if fout, err = os.Open(targetPath); err != nil {
return "", err
}
if osutil.StreamsEqual(fin, fout) {
fout.Close()
return targetPath, nil
}
fout.Close()
if err := os.Remove(targetPath); err != nil {
return "", err
}
// Ensure we're validating the Xauthority file from the beginning
if _, err := fin.Seek(int64(os.SEEK_SET), 0); err != nil {
return "", err
}
}
// To guard against setting XAUTHORITY to non-xauth files, check
// that we have a valid Xauthority. Specifically, the file must be
// parseable as an Xauthority file and not be empty.
if err := x11.ValidateXauthority(fin); err != nil {
return "", err
}
// Read data from the beginning of the file
if _, err = fin.Seek(int64(os.SEEK_SET), 0); err != nil {
return "", err
}
fout, err := os.OpenFile(targetPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return "", err
}
defer fout.Close()
// Read and write validated Xauthority file to its right location
if _, err = io.Copy(fout, fin); err != nil {
if err := os.Remove(targetPath); err != nil {
logger.Noticef("WARNING: cannot remove file at %s: %s", targetPath, err)
}
return "", fmt.Errorf(i18n.G("cannot write new Xauthority file at %s: %s"), targetPath, err)
}
return targetPath, nil
}
func runSnapConfine(info *snap.Info, securityTag, snapApp, command, hook string, args []string) error {
snapConfine := filepath.Join(dirs.DistroLibExecDir, "snap-confine")
// if we re-exec, we must run the snap-confine from the core snap
// as well, if they get out of sync, havoc will happen
if isReexeced() {
// run snap-confine from the core snap. that will work because
// snap-confine on the core snap is mostly statically linked
// (except libudev and libc)
snapConfine = filepath.Join(dirs.SnapMountDir, "core/current", dirs.CoreLibExecDir, "snap-confine")
}
if !osutil.FileExists(snapConfine) {
if hook != "" {
logger.Noticef("WARNING: skipping running hook %q of snap %q: missing snap-confine", hook, info.Name())
return nil
}
return fmt.Errorf(i18n.G("missing snap-confine: try updating your snapd package"))
}
if err := createUserDataDirs(info); err != nil {
logger.Noticef("WARNING: cannot create user data directory: %s", err)
}
xauthPath, err := migrateXauthority(info)
if err != nil {
logger.Noticef("WARNING: cannot copy user Xauthority file: %s", err)
}
cmd := []string{snapConfine}
if info.NeedsClassic() {
cmd = append(cmd, "--classic")
}
cmd = append(cmd, securityTag)
cmd = append(cmd, filepath.Join(dirs.CoreLibExecDir, "snap-exec"))
if command != "" {
cmd = append(cmd, "--command="+command)
}
if hook != "" {
cmd = append(cmd, "--hook="+hook)
}
// snap-exec is POSIXly-- options must come before positionals.
cmd = append(cmd, snapApp)
cmd = append(cmd, args...)
extraEnv := make(map[string]string)
if len(xauthPath) > 0 {
extraEnv["XAUTHORITY"] = xauthPath
}
env := snapenv.ExecEnv(info, extraEnv)
return syscallExec(cmd[0], cmd, env)
}