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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2024 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 kmod implements a backend which loads kernel modules on behalf of
// interfaces.
//
// Interfaces may request kernel modules to be loaded by providing snippets via
// their respective "*Snippet" methods for interfaces.SecurityKMod security
// system. The snippet should contain a newline-separated list of requested
// kernel modules. The KMod backend stores all the modules needed by given
// snap in /etc/modules-load.d/snap.<snapname>.conf file ensuring they are
// loaded when the system boots and also loads these modules via modprobe.
// If a snap is uninstalled or respective interface gets disconnected, the
// corresponding /etc/modules-load.d/ config file gets removed, however no
// kernel modules are unloaded. This is by design.
//
// Note: this mechanism should not be confused with kernel-module-interface;
// kmod only loads a well-defined list of modules provided by interface definition
// and doesn't grant any special permissions related to kernel modules to snaps,
// in contrast to kernel-module-interface.
package kmod
import (
"bytes"
"fmt"
"os"
"sort"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/timings"
)
// Backend is responsible for maintaining kernel modules
type Backend struct {
preseed bool
}
// Initialize does nothing.
func (b *Backend) Initialize(opts *interfaces.SecurityBackendOptions) error {
if opts != nil && opts.Preseed {
b.preseed = true
}
return nil
}
// Name returns the name of the backend.
func (b *Backend) Name() interfaces.SecuritySystem {
return "kmod"
}
// setupModules creates a conf file with list of kernel modules required by
// given snap, writes it in /etc/modules-load.d/ directory and immediately
// loads the modules using /sbin/modprobe. The devMode is ignored.
func (b *Backend) setupModules(appSet *interfaces.SnapAppSet, spec *Specification) error {
content, modules := deriveContent(spec, appSet)
// synchronize the content with the filesystem
globs := interfaces.SecurityTagGlobs(appSet.InstanceName())
dir := dirs.SnapKModModulesDir
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("cannot create directory for kmod files %q: %s", dir, err)
}
changed, _, err := osutil.EnsureDirStateGlobs(dirs.SnapKModModulesDir, globs, content)
if err != nil {
return err
}
if len(changed) > 0 {
b.loadModules(modules)
}
return nil
}
// setupModprobe creates a configuration file under /etc/modprobe.d/ according
// to the specification: this allows to either specify the load parameters for
// a module, or prevent it from being loaded.
// TODO: consider whether
// - a newly blocklisted module should get unloaded
// - a module whose option change should get reloaded
func (b *Backend) setupModprobe(appSet *interfaces.SnapAppSet, spec *Specification) error {
dir := dirs.SnapKModModprobeDir
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("cannot create directory for kmod files %q: %s", dir, err)
}
globs := interfaces.SecurityTagGlobs(appSet.InstanceName())
dirContents := prepareModprobeDirContents(spec, appSet)
_, _, err := osutil.EnsureDirStateGlobs(dirs.SnapKModModprobeDir, globs, dirContents)
if err != nil {
return err
}
return nil
}
// Setup will make the kmod backend generate the needed system files (such as
// those under /etc/modules-load.d/ and /etc/modprobe.d/) and call the
// appropriate system commands so that the desired kernel module configuration
// will be applied.
// The devMode is ignored.
//
// If the method fails it should be re-tried (with a sensible strategy) by the caller.
func (b *Backend) Setup(appSet *interfaces.SnapAppSet, opts interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) error {
snapName := appSet.InstanceName()
// Get the snippets that apply to this snap
spec, err := repo.SnapSpecification(b.Name(), appSet, opts)
if err != nil {
return fmt.Errorf("cannot obtain kmod specification for snap %q: %s", snapName, err)
}
err = b.setupModprobe(appSet, spec.(*Specification))
if err != nil {
return err
}
err = b.setupModules(appSet, spec.(*Specification))
if err != nil {
return err
}
return nil
}
// Remove removes modules config file specific to a given snap.
//
// This method should be called after removing a snap.
//
// If the method fails it should be re-tried (with a sensible strategy) by the caller.
func (b *Backend) Remove(snapName string) error {
globs := interfaces.SecurityTagGlobs(snapName)
var errors []error
if _, _, err := osutil.EnsureDirStateGlobs(dirs.SnapKModModulesDir, globs, nil); err != nil {
errors = append(errors, err)
}
if _, _, err := osutil.EnsureDirStateGlobs(dirs.SnapKModModprobeDir, globs, nil); err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
return fmt.Errorf("cannot remove kernel modules config files: %v", errors)
}
return nil
}
func deriveContent(spec *Specification, appSet *interfaces.SnapAppSet) (map[string]osutil.FileState, []string) {
if len(spec.modules) == 0 {
return nil, nil
}
content := make(map[string]osutil.FileState)
var modules []string
for k := range spec.modules {
modules = append(modules, k)
}
sort.Strings(modules)
var buffer bytes.Buffer
buffer.WriteString("# This file is automatically generated.\n")
for _, module := range modules {
buffer.WriteString(module)
buffer.WriteRune('\n')
}
content[fmt.Sprintf("%s.conf", snap.SecurityTag(appSet.InstanceName()))] = &osutil.MemoryFileState{
Content: buffer.Bytes(),
Mode: 0644,
}
return content, modules
}
func prepareModprobeDirContents(spec *Specification, appSet *interfaces.SnapAppSet) map[string]osutil.FileState {
disallowedModules := spec.DisallowedModules()
if len(disallowedModules) == 0 && len(spec.moduleOptions) == 0 {
return nil
}
contents := "# Generated by snapd. Do not edit\n\n"
// First, write down the list of disallowed modules
for _, module := range disallowedModules {
contents += fmt.Sprintf("blacklist %s\n", module)
}
// Then, write down the module options
for module, options := range spec.moduleOptions {
contents += fmt.Sprintf("options %s %s\n", module, options)
}
fileName := fmt.Sprintf("%s.conf", snap.SecurityTag(appSet.InstanceName()))
return map[string]osutil.FileState{
fileName: &osutil.MemoryFileState{
Content: []byte(contents),
Mode: 0644,
},
}
}
func (b *Backend) NewSpecification(*interfaces.SnapAppSet, interfaces.ConfinementOptions) interfaces.Specification {
return &Specification{}
}
// SandboxFeatures returns the list of features supported by snapd for loading kernel modules.
func (b *Backend) SandboxFeatures() []string {
return []string{"mediated-modprobe"}
}
|