File: argon2_out_of_process_sb.go

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (92 lines) | stat: -rw-r--r-- 2,996 bytes parent folder | download | duplicates (3)
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
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build !nosecboot

/*
 * Copyright (C) 2025 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 secboot

import (
	"fmt"
	"os"
	"os/exec"
	"time"

	sb "github.com/snapcore/secboot"

	"github.com/snapcore/snapd/logger"
)

var (
	osExit     = os.Exit
	osReadlink = os.Readlink

	sbWaitForAndRunArgon2OutOfProcessRequest = sb.WaitForAndRunArgon2OutOfProcessRequest
	sbNewOutOfProcessArgon2KDF               = sb.NewOutOfProcessArgon2KDF
	sbSetArgon2KDF                           = sb.SetArgon2KDF
)

const (
	outOfProcessArgon2KDFTimeout = 100 * time.Millisecond
)

// HijackAndRunArgon2OutOfProcessHandlerOnArg is supposed to be called from the
// main() of binaries involved with sealing/unsealing of keys (i.e. snapd and
// snap-bootstrap).
//
// This switches the binary to a special argon2 mode when the matching args are
// detected where it hijacks the process and acts as an argon2 out-of-process
// helper command and exits when its work is done, otherwise (in normal mode)
// it sets the default argon2 kdf implementation to be self-invoking into the
// special argon2 mode of the calling binary.
//
// For more context, check docs for sb.WaitForAndRunArgon2OutOfProcessRequest
// and sb.NewOutOfProcessArgon2KDF for details on how the flow works
// in secboot.
func HijackAndRunArgon2OutOfProcessHandlerOnArg(args []string) {
	if !isOutOfProcessArgon2KDFMode(args) {
		// Binary was invoked in normal mode, let's setup default argon2 kdf implementation
		// to point to this binary when invoked using special args.
		exe, err := osReadlink("/proc/self/exe")
		if err != nil {
			logger.Noticef("internal error: failed to read symlink of /proc/self/exe: %v", err)
			return
		}

		handlerCmd := func() (*exec.Cmd, error) {
			cmd := exec.Command(exe, args...)
			return cmd, nil
		}
		argon2KDF := sbNewOutOfProcessArgon2KDF(handlerCmd, outOfProcessArgon2KDFTimeout, nil)
		sbSetArgon2KDF(argon2KDF)

		return
	}

	logger.Noticef("running argon2 out-of-process helper")
	// Ignore the lock release callback and use implicit release on process termination.
	_, err := sbWaitForAndRunArgon2OutOfProcessRequest(os.Stdin, os.Stdout, sb.NoArgon2OutOfProcessWatchdogHandler())
	if err != nil {
		fmt.Fprintf(os.Stderr, "cannot run argon2 out-of-process request: %v", err)
		osExit(1)
	}

	// Argon2 request was processed successfully
	osExit(0)

	panic("internal error: not reachable")
}