Skip to content

Commit

Permalink
lib: Merge security related sources
Browse files Browse the repository at this point in the history
Merge FIPS and lockdown related library sources to new tst_security.[ch]
file to shorten number of the files in the library. More security
related code will be added in next commit.

Link: https://lore.kernel.org/ltp/[email protected]/
Reviewed-by: Cyril Hrubis <[email protected]>
Signed-off-by: Petr Vorel <[email protected]>
  • Loading branch information
pevik committed Mar 26, 2024
1 parent d1e7424 commit 67ab430
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 82 deletions.
15 changes: 0 additions & 15 deletions include/tst_fips.h

This file was deleted.

11 changes: 0 additions & 11 deletions include/tst_lockdown.h

This file was deleted.

17 changes: 17 additions & 0 deletions include/tst_security.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (c) Linux Test Project, 2020-2024
*/

#ifndef TST_SECURITY_H__
#define TST_SECURITY_H__

/*
* Detect whether FIPS enabled
* @return 0: FIPS not enabled, 1: FIPS enabled
*/
int tst_fips_enabled(void);

int tst_lockdown_enabled(void);
int tst_secureboot_enabled(void);

#endif /* TST_SECURITY_H__ */
3 changes: 1 addition & 2 deletions include/tst_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
#include "tst_capability.h"
#include "tst_hugepage.h"
#include "tst_assert.h"
#include "tst_lockdown.h"
#include "tst_fips.h"
#include "tst_security.h"
#include "tst_taint.h"
#include "tst_memutils.h"
#include "tst_arch.h"
Expand Down
24 changes: 0 additions & 24 deletions lib/tst_fips.c

This file was deleted.

73 changes: 43 additions & 30 deletions lib/tst_lockdown.c → lib/tst_security.c
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) Linux Test Project, 2020-2023
* Copyright (c) Linux Test Project, 2020-2024
*/

#define TST_NO_DEFAULT_MAIN

#define PATH_FIPS "/proc/sys/crypto/fips_enabled"
#define PATH_LOCKDOWN "/sys/kernel/security/lockdown"

#if defined(__powerpc64__) || defined(__ppc64__)
# define SECUREBOOT_VAR "/proc/device-tree/ibm,secure-boot"
# define VAR_DATA_SIZE 4
#else
# define SECUREBOOT_VAR "/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
# define VAR_DATA_SIZE 5
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>

#include "tst_test.h"
#include "tst_safe_macros.h"
#include "tst_safe_stdio.h"
#include "tst_lockdown.h"
#include "tst_security.h"
#include "tst_private.h"

#if defined(__powerpc64__) || defined(__ppc64__)
# define SECUREBOOT_VAR "/proc/device-tree/ibm,secure-boot"
# define VAR_DATA_SIZE 4
#else
# define SECUREBOOT_VAR "/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
# define VAR_DATA_SIZE 5
#endif

int tst_secureboot_enabled(void)
int tst_fips_enabled(void)
{
int fd;
char data[5];
int fips = 0;

if (access(SECUREBOOT_VAR, F_OK)) {
tst_res(TINFO, "SecureBoot sysfs file not available");
return -1;
if (access(PATH_FIPS, R_OK) == 0) {
SAFE_FILE_SCANF(PATH_FIPS, "%d", &fips);
}

fd = open(SECUREBOOT_VAR, O_RDONLY);

if (fd == -1) {
tst_res(TINFO | TERRNO,
"Cannot open SecureBoot file");
return -1;
} else if (fd < 0) {
tst_brk(TBROK | TERRNO, "Invalid open() return value %d", fd);
return -1;
}
SAFE_READ(1, fd, data, VAR_DATA_SIZE);
SAFE_CLOSE(fd);
tst_res(TINFO, "SecureBoot: %s", data[VAR_DATA_SIZE - 1] ? "on" : "off");
return data[VAR_DATA_SIZE - 1];
tst_res(TINFO, "FIPS: %s", fips ? "on" : "off");
return fips;
}

int tst_lockdown_enabled(void)
Expand Down Expand Up @@ -86,3 +73,29 @@ int tst_lockdown_enabled(void)

return ret;
}

int tst_secureboot_enabled(void)
{
int fd;
char data[5];

if (access(SECUREBOOT_VAR, F_OK)) {
tst_res(TINFO, "SecureBoot sysfs file not available");
return -1;
}

fd = open(SECUREBOOT_VAR, O_RDONLY);

if (fd == -1) {
tst_res(TINFO | TERRNO,
"Cannot open SecureBoot file");
return -1;
} else if (fd < 0) {
tst_brk(TBROK | TERRNO, "Invalid open() return value %d", fd);
return -1;
}
SAFE_READ(1, fd, data, VAR_DATA_SIZE);
SAFE_CLOSE(fd);
tst_res(TINFO, "SecureBoot: %s", data[VAR_DATA_SIZE - 1] ? "on" : "off");
return data[VAR_DATA_SIZE - 1];
}

0 comments on commit 67ab430

Please sign in to comment.