Skip to content

Commit 5050f7b

Browse files
WenhuaChanglsandov1
authored andcommitted
disk/cryptodisk: Require authentication after TPM unlock for CLI access
The GRUB may use TPM to verify the integrity of boot components and the result can determine whether a previously sealed key can be released. If everything checks out, showing nothing has been tampered with, the key is released and GRUB unlocks the encrypted root partition for the next stage of booting. However, the liberal Command Line Interface (CLI) can be misused by anyone in this case to access files in the encrypted partition one way or another. Despite efforts to keep the CLI secure by preventing utility command output from leaking file content, many techniques in the wild could still be used to exploit the CLI, enabling attacks or learning methods to attack. It's nearly impossible to account for all scenarios where a hack could be applied. Therefore, to mitigate potential misuse of the CLI after the root device has been successfully unlocked via TPM, the user should be required to authenticate using the LUKS password. This added layer of security ensures that only authorized users can access the CLI reducing the risk of exploitation or unauthorized access to the encrypted partition. Fixes: CVE-2024-49504 Signed-off-by: Michael Chang <mchang@suse.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1 parent 7ab94e0 commit 5050f7b

File tree

9 files changed

+171
-1
lines changed

9 files changed

+171
-1
lines changed

docs/grub.texi

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6882,6 +6882,35 @@ sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed
68826882
As with UEFI secure boot, it is necessary to build in the required modules,
68836883
or sign them separately.
68846884

6885+
@subsection Command line and menuentry editor protection
6886+
6887+
The TPM key protector provides full disk encryption support on servers or
6888+
virtual machine images, meanwhile keeping the boot process unattended. This
6889+
prevents service disruptions by eliminating the need for manual password input
6890+
during startup, improving system uptime and continuity. It is achieved by TPM,
6891+
which verifies the integrity of boot components by checking cryptographic
6892+
hashes against securely stored values, to confirm the disks are unlocked in a
6893+
trusted state.
6894+
6895+
However, for users to access the system interactively, some form of
6896+
authentication is still required, as the disks are not unlocked by an
6897+
authorized user. This raised concerns about using an unprotected
6898+
@samp{command-line interface} (@pxref{Command-line interface}), as anyone could
6899+
execute commands to access decrypted data. To address this issue, the LUKS
6900+
password is used to ensure that only authorized users are granted access to the
6901+
interface. Additionally, the @samp{menu entry editor} (@pxref{Menu entry
6902+
editor}) is also safeguarded by the LUKS password, as modifying a boot entry is
6903+
effectively the same as altering the @file{grub.cfg} file read from encrypted
6904+
files.
6905+
6906+
It is worth mentioning that the built-in password support, as described in
6907+
@samp{Authentication and Authorization in GRUB} (@pxref{Authentication and
6908+
authorisation}), can also be used to protect the command-line interface from
6909+
unauthorized access. However, it is not recommended to rely on this approach as
6910+
it is an optional step. Setting it up requires additional manual intervention,
6911+
which increases the risk of password leakage during the process. Moreover, the
6912+
superuser list must be well maintained, and the password used cannot be
6913+
synchronized with LUKS key rotation.
68856914

68866915
@node Platform limitations
68876916
@chapter Platform limitations

grub-core/disk/cryptodisk.c

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,9 @@ grub_cryptodisk_scan_device_real (const char *name,
11441144
ret = grub_cryptodisk_insert (dev, name, source);
11451145
if (ret != GRUB_ERR_NONE)
11461146
goto error;
1147-
1147+
#ifndef GRUB_UTIL
1148+
grub_cli_set_auth_needed ();
1149+
#endif
11481150
goto cleanup;
11491151
}
11501152
grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
@@ -1576,6 +1578,89 @@ luks_script_get (grub_size_t *sz)
15761578
return ret;
15771579
}
15781580

1581+
#ifdef GRUB_MACHINE_EFI
1582+
grub_err_t
1583+
grub_cryptodisk_challenge_password (void)
1584+
{
1585+
grub_cryptodisk_t cr_dev;
1586+
1587+
for (cr_dev = cryptodisk_list; cr_dev != NULL; cr_dev = cr_dev->next)
1588+
{
1589+
grub_cryptodisk_dev_t cr;
1590+
grub_disk_t source = NULL;
1591+
grub_err_t ret = GRUB_ERR_NONE;
1592+
grub_cryptodisk_t dev = NULL;
1593+
char *part = NULL;
1594+
struct grub_cryptomount_args cargs = {0};
1595+
1596+
cargs.check_boot = 0;
1597+
cargs.search_uuid = cr_dev->uuid;
1598+
1599+
source = grub_disk_open (cr_dev->source);
1600+
1601+
if (source == NULL)
1602+
{
1603+
ret = grub_errno;
1604+
goto error_out;
1605+
}
1606+
1607+
FOR_CRYPTODISK_DEVS (cr)
1608+
{
1609+
dev = cr->scan (source, &cargs);
1610+
if (grub_errno)
1611+
{
1612+
ret = grub_errno;
1613+
goto error_out;
1614+
}
1615+
if (dev == NULL)
1616+
continue;
1617+
break;
1618+
}
1619+
1620+
if (dev == NULL)
1621+
{
1622+
ret = grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
1623+
goto error_out;
1624+
}
1625+
1626+
part = grub_partition_get_name (source->partition);
1627+
grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
1628+
source->partition != NULL ? "," : "",
1629+
part != NULL ? part : N_("UNKNOWN"), cr_dev->uuid);
1630+
grub_free (part);
1631+
1632+
cargs.key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
1633+
if (cargs.key_data == NULL)
1634+
{
1635+
ret = grub_errno;
1636+
goto error_out;
1637+
}
1638+
1639+
if (!grub_password_get ((char *) cargs.key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
1640+
{
1641+
ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
1642+
goto error_out;
1643+
}
1644+
cargs.key_len = grub_strlen ((char *) cargs.key_data);
1645+
ret = cr->recover_key (source, dev, &cargs);
1646+
1647+
error_out:
1648+
grub_disk_close (source);
1649+
if (dev != NULL)
1650+
cryptodisk_close (dev);
1651+
if (cargs.key_data)
1652+
{
1653+
grub_memset (cargs.key_data, 0, cargs.key_len);
1654+
grub_free (cargs.key_data);
1655+
}
1656+
1657+
return ret;
1658+
}
1659+
1660+
return GRUB_ERR_NONE;
1661+
}
1662+
#endif /* GRUB_MACHINE_EFI */
1663+
15791664
struct grub_procfs_entry luks_script =
15801665
{
15811666
.name = "luks_script",

grub-core/kern/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#endif
3838

3939
static bool cli_disabled = false;
40+
static bool cli_need_auth = false;
4041

4142
grub_addr_t
4243
grub_modules_get_end (void)
@@ -308,6 +309,17 @@ grub_is_cli_disabled (void)
308309
return cli_disabled;
309310
}
310311

312+
bool
313+
grub_is_cli_need_auth (void)
314+
{
315+
return cli_need_auth;
316+
}
317+
318+
void grub_cli_set_auth_needed (void)
319+
{
320+
cli_need_auth = true;
321+
}
322+
311323
static void
312324
check_is_cli_disabled (void)
313325
{

grub-core/normal/auth.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <grub/time.h>
2626
#include <grub/i18n.h>
2727

28+
#ifdef GRUB_MACHINE_EFI
29+
#include <grub/cryptodisk.h>
30+
#endif
31+
2832
struct grub_auth_user
2933
{
3034
struct grub_auth_user *next;
@@ -200,6 +204,32 @@ grub_username_get (char buf[], unsigned buf_size)
200204
return (key != GRUB_TERM_ESC);
201205
}
202206

207+
grub_err_t
208+
grub_auth_check_cli_access (void)
209+
{
210+
if (grub_is_cli_need_auth () == true)
211+
{
212+
#ifdef GRUB_MACHINE_EFI
213+
static bool authenticated = false;
214+
215+
if (authenticated == false)
216+
{
217+
grub_err_t ret;
218+
219+
ret = grub_cryptodisk_challenge_password ();
220+
if (ret == GRUB_ERR_NONE)
221+
authenticated = true;
222+
return ret;
223+
}
224+
return GRUB_ERR_NONE;
225+
#else
226+
return GRUB_ACCESS_DENIED;
227+
#endif
228+
}
229+
230+
return GRUB_ERR_NONE;
231+
}
232+
203233
grub_err_t
204234
grub_auth_check_authentication (const char *userlist)
205235
{

grub-core/normal/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,13 @@ grub_cmdline_run (int nested, int force_auth)
557557
}
558558
while (err && force_auth);
559559

560+
if (err == GRUB_ERR_NONE)
561+
err = grub_auth_check_cli_access ();
562+
560563
if (err)
561564
{
562565
grub_print_error ();
566+
grub_wait_after_message ();
563567
grub_errno = GRUB_ERR_NONE;
564568
return;
565569
}

grub-core/normal/menu_entry.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,13 @@ grub_menu_entry_run (grub_menu_entry_t entry)
12551255

12561256
err = grub_auth_check_authentication (NULL);
12571257

1258+
if (err == GRUB_ERR_NONE)
1259+
err = grub_auth_check_cli_access ();
1260+
12581261
if (err)
12591262
{
12601263
grub_print_error ();
1264+
grub_wait_after_message ();
12611265
grub_errno = GRUB_ERR_NONE;
12621266
return;
12631267
}

include/grub/auth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ grub_err_t grub_auth_unregister_authentication (const char *user);
3333
grub_err_t grub_auth_authenticate (const char *user);
3434
grub_err_t grub_auth_deauthenticate (const char *user);
3535
grub_err_t grub_auth_check_authentication (const char *userlist);
36+
grub_err_t grub_auth_check_cli_access (void);
3637

3738
#endif /* ! GRUB_AUTH_HEADER */

include/grub/cryptodisk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,7 @@ grub_util_get_geli_uuid (const char *dev);
187187
grub_cryptodisk_t grub_cryptodisk_get_by_uuid (const char *uuid);
188188
grub_cryptodisk_t grub_cryptodisk_get_by_source_disk (grub_disk_t disk);
189189

190+
#ifdef GRUB_MACHINE_EFI
191+
grub_err_t grub_cryptodisk_challenge_password (void);
192+
#endif
190193
#endif

include/grub/misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
443443
grub_uint64_t *r);
444444

445445
extern bool EXPORT_FUNC(grub_is_cli_disabled) (void);
446+
extern bool EXPORT_FUNC(grub_is_cli_need_auth) (void);
447+
extern void EXPORT_FUNC(grub_cli_set_auth_needed) (void);
446448

447449
/* Must match softdiv group in gentpl.py. */
448450
#if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__) || \

0 commit comments

Comments
 (0)