|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +script to test fscrypt support |
| 4 | +
|
| 5 | +Copyright (C) 2025 Canonical Ltd. |
| 6 | +
|
| 7 | +Authors |
| 8 | + Alexis Cellier <[email protected]> |
| 9 | +
|
| 10 | +This program is free software: you can redistribute it and/or modify |
| 11 | +it under the terms of the GNU General Public License version 3, |
| 12 | +as published by the Free Software Foundation. |
| 13 | +
|
| 14 | +This program is distributed in the hope that it will be useful, |
| 15 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +GNU General Public License for more details. |
| 18 | +
|
| 19 | +You should have received a copy of the GNU General Public License |
| 20 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +
|
| 22 | +The purpose of this scrupt is to make a small fscrupt test on a generated |
| 23 | +ext4 disk image to validate the filesystem encryption support. |
| 24 | +""" |
| 25 | + |
| 26 | +import os |
| 27 | +import subprocess |
| 28 | +import tempfile |
| 29 | +import re |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + """ |
| 35 | + Create an ext4 disk image, mount it, setup fscrypt and use it with a simple |
| 36 | + file |
| 37 | + """ |
| 38 | + with tempfile.TemporaryDirectory() as tmp_path: |
| 39 | + tmp = Path(tmp_path) |
| 40 | + mnt = tmp / "mnt" |
| 41 | + img = tmp / "fs.img" |
| 42 | + key_file = tmp / "key" |
| 43 | + test_dir = mnt / "test" |
| 44 | + test_file = test_dir / "test.txt" |
| 45 | + fscrypt_config = Path("/etc/fscrypt.conf") |
| 46 | + fscrypt_setup = fscrypt_config.exists() |
| 47 | + |
| 48 | + # Create a 50MB file |
| 49 | + subprocess.check_call(["truncate", "-s", "50M", str(img)]) |
| 50 | + |
| 51 | + mnt.mkdir(parents=True, exist_ok=True) |
| 52 | + |
| 53 | + # Make ext4 image with encryption support |
| 54 | + subprocess.check_call( |
| 55 | + ["mkfs.ext4", "-F", "-O", "encrypt,stable_inodes", str(img)] |
| 56 | + ) |
| 57 | + |
| 58 | + # Mount it |
| 59 | + subprocess.check_call(["mount", str(img), str(mnt)]) |
| 60 | + |
| 61 | + try: |
| 62 | + # Setup fscrypt |
| 63 | + print("Setup fscrupt") |
| 64 | + if not fscrypt_setup: |
| 65 | + subprocess.run( |
| 66 | + ["fscrypt", "setup", "--force"], |
| 67 | + input="n\n", |
| 68 | + text=True, |
| 69 | + check=True, |
| 70 | + ) |
| 71 | + subprocess.run( |
| 72 | + ["fscrypt", "setup", "--force", str(mnt)], |
| 73 | + input="n\n", |
| 74 | + text=True, |
| 75 | + check=True, |
| 76 | + ) |
| 77 | + |
| 78 | + # Confirm fscrypt is enabled |
| 79 | + output = subprocess.check_output( |
| 80 | + ["fscrypt", "status"], |
| 81 | + text=True, |
| 82 | + ) |
| 83 | + found = False |
| 84 | + pattern = re.compile(rf"^{re.escape(str(mnt))}.*supported *Yes$") |
| 85 | + for line in output.splitlines(): |
| 86 | + if pattern.match(line): |
| 87 | + found = True |
| 88 | + break |
| 89 | + if not found: |
| 90 | + raise SystemExit("Failed to setup fscrypt") |
| 91 | + |
| 92 | + # Write random key |
| 93 | + with key_file.open("wb") as f: |
| 94 | + f.write(os.urandom(32)) |
| 95 | + |
| 96 | + # Make test directory |
| 97 | + test_dir.mkdir(parents=True, exist_ok=True) |
| 98 | + |
| 99 | + # Encrypt directory |
| 100 | + subprocess.check_call( |
| 101 | + [ |
| 102 | + "fscrypt", |
| 103 | + "encrypt", |
| 104 | + "--quiet", |
| 105 | + "--source=raw_key", |
| 106 | + "--name=test_key", |
| 107 | + f"--key={str(key_file)}", |
| 108 | + str(test_dir), |
| 109 | + ] |
| 110 | + ) |
| 111 | + |
| 112 | + # Write a file inside |
| 113 | + with test_file.open("w") as f: |
| 114 | + f.write("test\n") |
| 115 | + |
| 116 | + # Lock the directory |
| 117 | + subprocess.check_call(["fscrypt", "lock", str(test_dir)]) |
| 118 | + |
| 119 | + # Should not be able to list the file |
| 120 | + if test_file.exists(): |
| 121 | + raise SystemExit("File should not be accessible when locked") |
| 122 | + print("File correctly inaccessible when locked") |
| 123 | + |
| 124 | + # Unlock the directory |
| 125 | + subprocess.check_call( |
| 126 | + ["fscrypt", "unlock", f"--key={str(key_file)}", str(test_dir)] |
| 127 | + ) |
| 128 | + |
| 129 | + with test_file.open("r") as f: |
| 130 | + content = f.read().strip() |
| 131 | + if content != "test": |
| 132 | + raise SystemExit("File contents not correct after unlock") |
| 133 | + print("File is accessible and content is correct after unlock") |
| 134 | + finally: |
| 135 | + subprocess.check_call(["umount", str(mnt)]) |
| 136 | + if not fscrypt_setup and fscrypt_config.exists(): |
| 137 | + fscrypt_config.unlink() |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments