2019-12-13 11:41:32 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
package cryptsetup
|
2019-12-13 11:41:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-10-17 11:55:08 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2019-12-13 11:41:32 +00:00
|
|
|
"fmt"
|
2024-06-21 10:19:06 +00:00
|
|
|
"os"
|
2019-12-13 11:41:32 +00:00
|
|
|
"os/exec"
|
util: Limit cryptsetup PBKDF memory usage
By default, `cryptsetup luksFormat` uses Argon2i as Password-Based Key
Derivation Function (PBKDF), which not only has a CPU cost, but also a memory
cost (to make brute-force attacks harder).
The memory cost is based on the available system memory by default, which in
the context of Ceph CSI can be a problem for two reasons:
1. Pods can have a memory limit (much lower that the memory available on the
node, usually) which isn't taken into account by `cryptsetup`, so it can get
OOM-killed when formating a new volume;
2. The amount of memory that was used during `cryptsetup luksFormat` will then
be needed for `cryptsetup luksOpen`, so if the volume was formated on a node
with a lot of memory, but then needs to be opened on a different node with
less memory, `cryptsetup` will get OOM-killed.
This commit sets the PBKDF memory limit to a fixed value to ensure consistent
memory usage regardless of the specifications of the nodes where the volume
happens to be formatted in the first place.
The limit is set to a relatively low value (32 MiB) so that the `csi-rbdplugin`
container in the `nodeplugin` pod doesn't require an extravagantly high memory
limit in order to format/open volumes (particularly with operations happening
in parallel), while at the same time not being so low as to render it
completely pointless.
Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
2023-04-26 09:22:25 +00:00
|
|
|
"strconv"
|
2019-12-13 11:41:32 +00:00
|
|
|
"strings"
|
2024-10-17 11:55:08 +00:00
|
|
|
"time"
|
2024-06-21 10:19:06 +00:00
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/internal/util/file"
|
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2024-10-17 11:55:08 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/stripsecrets"
|
2019-12-13 11:41:32 +00:00
|
|
|
)
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
const (
|
|
|
|
// Maximum time to wait for cryptsetup commands to complete.
|
|
|
|
ExecutionTimeout = 2*time.Minute + 30*time.Second
|
|
|
|
|
|
|
|
// Limit memory used by Argon2i PBKDF to 32 MiB.
|
|
|
|
pkdbfMemoryLimit = 32 << 10 // 32768 KiB
|
|
|
|
)
|
|
|
|
|
|
|
|
// LuksWrapper is a struct that provides a context-aware wrapper around cryptsetup commands.
|
|
|
|
type LUKSWrapper interface {
|
|
|
|
Format(devicePath, passphrase string) (string, string, error)
|
|
|
|
Open(devicePath, mapperFile, passphrase string) (string, string, error)
|
|
|
|
Close(mapperFile string) (string, string, error)
|
|
|
|
AddKey(devicePath, passphrase, newPassphrase, slot string) error
|
|
|
|
RemoveKey(devicePath, passphrase, slot string) error
|
|
|
|
Resize(mapperFile string) (string, string, error)
|
|
|
|
VerifyKey(devicePath, passphrase, slot string) (bool, error)
|
|
|
|
Status(mapperFile string) (string, string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// luksWrapper is a type that implements LUKSWrapper interface
|
|
|
|
// and provides a shared context for its methods.
|
|
|
|
type luksWrapper struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLUKSWrapper creates a new LUKSWrapper instance with the provided context.
|
|
|
|
// The context is used to control the lifetime of the cryptsetup commands.
|
|
|
|
func NewLUKSWrapper(ctx context.Context) LUKSWrapper {
|
|
|
|
return &luksWrapper{ctx: ctx}
|
|
|
|
}
|
util: Limit cryptsetup PBKDF memory usage
By default, `cryptsetup luksFormat` uses Argon2i as Password-Based Key
Derivation Function (PBKDF), which not only has a CPU cost, but also a memory
cost (to make brute-force attacks harder).
The memory cost is based on the available system memory by default, which in
the context of Ceph CSI can be a problem for two reasons:
1. Pods can have a memory limit (much lower that the memory available on the
node, usually) which isn't taken into account by `cryptsetup`, so it can get
OOM-killed when formating a new volume;
2. The amount of memory that was used during `cryptsetup luksFormat` will then
be needed for `cryptsetup luksOpen`, so if the volume was formated on a node
with a lot of memory, but then needs to be opened on a different node with
less memory, `cryptsetup` will get OOM-killed.
This commit sets the PBKDF memory limit to a fixed value to ensure consistent
memory usage regardless of the specifications of the nodes where the volume
happens to be formatted in the first place.
The limit is set to a relatively low value (32 MiB) so that the `csi-rbdplugin`
container in the `nodeplugin` pod doesn't require an extravagantly high memory
limit in order to format/open volumes (particularly with operations happening
in parallel), while at the same time not being so low as to render it
completely pointless.
Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
2023-04-26 09:22:25 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksFormat sets up volume as an encrypted LUKS partition.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) Format(devicePath, passphrase string) (string, string, error) {
|
|
|
|
return l.execCryptsetupCommand(
|
2021-06-25 12:15:08 +00:00
|
|
|
&passphrase,
|
|
|
|
"-q",
|
|
|
|
"luksFormat",
|
|
|
|
"--type",
|
|
|
|
"luks2",
|
|
|
|
"--hash",
|
|
|
|
"sha256",
|
util: Limit cryptsetup PBKDF memory usage
By default, `cryptsetup luksFormat` uses Argon2i as Password-Based Key
Derivation Function (PBKDF), which not only has a CPU cost, but also a memory
cost (to make brute-force attacks harder).
The memory cost is based on the available system memory by default, which in
the context of Ceph CSI can be a problem for two reasons:
1. Pods can have a memory limit (much lower that the memory available on the
node, usually) which isn't taken into account by `cryptsetup`, so it can get
OOM-killed when formating a new volume;
2. The amount of memory that was used during `cryptsetup luksFormat` will then
be needed for `cryptsetup luksOpen`, so if the volume was formated on a node
with a lot of memory, but then needs to be opened on a different node with
less memory, `cryptsetup` will get OOM-killed.
This commit sets the PBKDF memory limit to a fixed value to ensure consistent
memory usage regardless of the specifications of the nodes where the volume
happens to be formatted in the first place.
The limit is set to a relatively low value (32 MiB) so that the `csi-rbdplugin`
container in the `nodeplugin` pod doesn't require an extravagantly high memory
limit in order to format/open volumes (particularly with operations happening
in parallel), while at the same time not being so low as to render it
completely pointless.
Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
2023-04-26 09:22:25 +00:00
|
|
|
"--pbkdf-memory",
|
2024-10-17 11:55:08 +00:00
|
|
|
strconv.Itoa(pkdbfMemoryLimit),
|
2021-06-25 12:15:08 +00:00
|
|
|
devicePath,
|
|
|
|
"-d",
|
|
|
|
"/dev/stdin")
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksOpen opens LUKS encrypted partition and sets up a mapping.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) Open(devicePath, mapperFile, passphrase string) (string, string, error) {
|
2021-07-08 15:06:42 +00:00
|
|
|
// cryptsetup option --disable-keyring (introduced with cryptsetup v2.0.0)
|
|
|
|
// will be ignored with luks1
|
2024-10-17 11:55:08 +00:00
|
|
|
return l.execCryptsetupCommand(
|
|
|
|
&passphrase,
|
|
|
|
"luksOpen",
|
|
|
|
devicePath,
|
|
|
|
mapperFile,
|
|
|
|
"--disable-keyring",
|
|
|
|
"-d",
|
|
|
|
"/dev/stdin")
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 07:31:08 +00:00
|
|
|
// LuksResize resizes LUKS encrypted partition.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) Resize(mapperFile string) (string, string, error) {
|
|
|
|
return l.execCryptsetupCommand(nil, "resize", mapperFile)
|
2021-07-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksClose removes existing mapping.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) Close(mapperFile string) (string, string, error) {
|
|
|
|
return l.execCryptsetupCommand(nil, "luksClose", mapperFile)
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksStatus returns encryption status of a provided device.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) Status(mapperFile string) (string, string, error) {
|
|
|
|
return l.execCryptsetupCommand(nil, "status", mapperFile)
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 10:19:06 +00:00
|
|
|
// LuksAddKey adds a new key to the specified slot.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) AddKey(devicePath, passphrase, newPassphrase, slot string) error {
|
2024-06-21 10:19:06 +00:00
|
|
|
passFile, err := file.CreateTempFile("luks-", passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.Remove(passFile.Name())
|
|
|
|
|
|
|
|
newPassFile, err := file.CreateTempFile("luks-", newPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.Remove(newPassFile.Name())
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
_, stderr, err := l.execCryptsetupCommand(
|
2024-06-21 10:19:06 +00:00
|
|
|
nil,
|
|
|
|
"--verbose",
|
|
|
|
"--key-file="+passFile.Name(),
|
|
|
|
"--key-slot="+slot,
|
|
|
|
"luksAddKey",
|
|
|
|
devicePath,
|
|
|
|
newPassFile.Name(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return early if no error to save us some time
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible scenarios
|
|
|
|
// 1. The provided passphrase to unlock the disk is wrong
|
|
|
|
// 2. The key slot is already in use
|
|
|
|
// If so, check if the key we want to add to the slot is already there
|
|
|
|
// If not, remove it and then add the new key to the slot
|
|
|
|
if strings.Contains(stderr, fmt.Sprintf("Key slot %s is full", slot)) {
|
|
|
|
// The given slot already has a key
|
|
|
|
// Check if it is the one that we want to update with
|
2024-10-17 11:55:08 +00:00
|
|
|
exists, fErr := l.VerifyKey(devicePath, newPassphrase, slot)
|
2024-06-21 10:19:06 +00:00
|
|
|
if fErr != nil {
|
|
|
|
return fErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verification passed, return early
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else, we remove the key from the given slot and add the new one
|
|
|
|
// Note: we use existing passphrase here as we are not yet sure if
|
|
|
|
// the newPassphrase is present in the headers
|
2024-10-17 11:55:08 +00:00
|
|
|
fErr = l.RemoveKey(devicePath, passphrase, slot)
|
2024-06-21 10:19:06 +00:00
|
|
|
if fErr != nil {
|
|
|
|
return fErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the slot is free, add the new key to it
|
2024-10-17 11:55:08 +00:00
|
|
|
fErr = l.AddKey(devicePath, passphrase, newPassphrase, slot)
|
2024-06-21 10:19:06 +00:00
|
|
|
if fErr != nil {
|
|
|
|
return fErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// No errors, we good.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The existing passphrase is wrong and the slot is empty
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LuksRemoveKey removes the key by killing the specified slot.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) RemoveKey(devicePath, passphrase, slot string) error {
|
2024-06-21 10:19:06 +00:00
|
|
|
keyFile, err := file.CreateTempFile("luks-", passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.Remove(keyFile.Name())
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
_, stderr, err := l.execCryptsetupCommand(
|
2024-06-21 10:19:06 +00:00
|
|
|
nil,
|
|
|
|
"--verbose",
|
|
|
|
"--key-file="+keyFile.Name(),
|
|
|
|
"luksKillSlot",
|
|
|
|
devicePath,
|
|
|
|
slot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// If a slot is not active, don't treat that as an error
|
|
|
|
if !strings.Contains(stderr, fmt.Sprintf("Keyslot %s is not active.", slot)) {
|
|
|
|
return fmt.Errorf("failed to kill slot %s for device %s: %w", slot, devicePath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LuksVerifyKey verifies that a key exists in a given slot.
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) VerifyKey(devicePath, passphrase, slot string) (bool, error) {
|
2024-06-21 10:19:06 +00:00
|
|
|
// Create a temp file that we will use to open the device
|
|
|
|
keyFile, err := file.CreateTempFile("luks-", passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer os.Remove(keyFile.Name())
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
_, stderr, err := l.execCryptsetupCommand(
|
2024-06-21 10:19:06 +00:00
|
|
|
nil,
|
|
|
|
"--verbose",
|
|
|
|
"--key-file="+keyFile.Name(),
|
|
|
|
"--key-slot="+slot,
|
|
|
|
"luksChangeKey",
|
|
|
|
devicePath,
|
|
|
|
keyFile.Name(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// If the passphrase doesn't match the key in given slot
|
|
|
|
if strings.Contains(stderr, "No key available with this passphrase.") {
|
|
|
|
// No match, no error
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise it was something else, return the wrapped error
|
|
|
|
log.ErrorLogMsg("failed to verify key in slot %s. stderr: %s. err: %v", slot, stderr, err)
|
|
|
|
|
|
|
|
return false, fmt.Errorf("failed to verify key in slot %s for device %s: %w", slot, devicePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
func (l *luksWrapper) execCryptsetupCommand(stdin *string, args ...string) (string, string, error) {
|
2019-12-13 11:41:32 +00:00
|
|
|
var (
|
|
|
|
program = "cryptsetup"
|
2024-10-17 11:55:08 +00:00
|
|
|
cmd = exec.CommandContext(l.ctx, program, args...) // #nosec:G204, commands executing not vulnerable.
|
|
|
|
sanitizedArgs = stripsecrets.InArgs(args)
|
2019-12-13 11:41:32 +00:00
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
if stdin != nil {
|
|
|
|
cmd.Stdin = strings.NewReader(*stdin)
|
|
|
|
}
|
2021-11-08 07:56:23 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
stdout := stdoutBuf.String()
|
|
|
|
stderr := stderrBuf.String()
|
2019-12-13 11:41:32 +00:00
|
|
|
|
2024-10-17 11:55:08 +00:00
|
|
|
if errors.Is(l.ctx.Err(), context.DeadlineExceeded) {
|
|
|
|
return stdout, stderr, fmt.Errorf("timeout occurred while running %s args: %v", program, sanitizedArgs)
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return stdout, stderr, fmt.Errorf("an error (%v)"+
|
2019-12-13 11:41:32 +00:00
|
|
|
" occurred while running %s args: %v", err, program, sanitizedArgs)
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:56:23 +00:00
|
|
|
return stdout, stderr, err
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|