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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2022 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 ctlcmd
import (
"errors"
"fmt"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/osutil/kmod"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/ifacestate"
)
var (
shortKmodHelp = i18n.G("Load or unload kernel modules")
longKmodHelp = i18n.G(`
The kmod command handles loading and unloading of kernel modules.`)
kmodLoadModule = kmod.LoadModule
kmodUnloadModule = kmod.UnloadModule
)
func init() {
addCommand("kmod", shortKmodHelp, longKmodHelp, func() command {
cmd := &kmodCommand{}
cmd.InsertCmd.kmod = cmd
cmd.RemoveCmd.kmod = cmd
return cmd
})
}
func (m *kmodCommand) Execute([]string) error {
// This is needed in order to implement the interface, but it's never
// called.
return nil
}
type kmodCommand struct {
baseCommand
InsertCmd KModInsertCmd `command:"insert" description:"load a kernel module"`
RemoveCmd KModRemoveCmd `command:"remove" description:"unload a kernel module"`
}
type KModInsertCmd struct {
Positional struct {
Module string `positional-arg-name:"<module>" required:"yes" description:"kernel module name"`
Options []string `positional-arg-name:"<options>" description:"kernel module options"`
} `positional-args:"yes" required:"yes"`
kmod *kmodCommand
}
func (k *KModInsertCmd) Execute([]string) error {
context, err := k.kmod.ensureContext()
if err != nil {
return err
}
if err := kmodCheckConnection(context, k.Positional.Module, k.Positional.Options); err != nil {
return fmt.Errorf("cannot load module %q: %v", k.Positional.Module, err)
}
if err := kmodLoadModule(k.Positional.Module, k.Positional.Options); err != nil {
return fmt.Errorf("cannot load module %q: %v", k.Positional.Module, err)
}
return nil
}
type KModRemoveCmd struct {
Positional struct {
Module string `positional-arg-name:"<module>" required:"yes" description:"kernel module name"`
} `positional-args:"yes" required:"yes"`
kmod *kmodCommand
}
func (k *KModRemoveCmd) Execute([]string) error {
context, err := k.kmod.ensureContext()
if err != nil {
return err
}
if err := kmodCheckConnection(context, k.Positional.Module, []string{}); err != nil {
return fmt.Errorf("cannot unload module %q: %v", k.Positional.Module, err)
}
if err := kmodUnloadModule(k.Positional.Module); err != nil {
return fmt.Errorf("cannot unload module %q: %v", k.Positional.Module, err)
}
return nil
}
// kmodMatchConnection checks whether the given kmod connection attributes give
// the snap permission to execute the kmod command
func kmodMatchConnection(attributes map[string]any, moduleName string, moduleOptions []string) bool {
load, found := attributes["load"]
if !found || load.(string) != "dynamic" {
return false
}
if moduleName != attributes["name"].(string) {
return false
}
if len(moduleOptions) > 0 {
// snapctl can be invoked with options only if the "options" attribute
// on the plug is set to "*"
optionsAttr, ok := attributes["options"]
if !ok || optionsAttr.(string) != "*" {
return false
}
}
return true
}
// kmodCheckConnection walks through the established connections to find one which
// is compatible with a kmod operation on the given moduleName and
// moduleOptions. Returns an error if not found.
var kmodCheckConnection = func(context *hookstate.Context, moduleName string, moduleOptions []string) (err error) {
snapName := context.InstanceName()
st := context.State()
st.Lock()
defer st.Unlock()
conns, err := ifacestate.ConnectionStates(st)
if err != nil {
return fmt.Errorf("internal error: cannot get connections: %s", err)
}
for connId, connState := range conns {
if connState.Interface != "kernel-module-load" {
continue
}
if !connState.Active() {
continue
}
connRef, err := interfaces.ParseConnRef(connId)
if err != nil {
return err
}
if connRef.PlugRef.Snap != snapName {
continue
}
modules, ok := connState.StaticPlugAttrs["modules"].([]any)
if !ok {
continue
}
for _, moduleAttributes := range modules {
attributes := moduleAttributes.(map[string]any)
if kmodMatchConnection(attributes, moduleName, moduleOptions) {
return nil
}
}
}
return errors.New("required interface not connected")
}
|