| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | /* |
| 3 | * License: GPLv3+ |
| 4 | * Copyright (c) 2024 Davide Madrisan <[email protected]> |
| 5 | * |
| 6 | * A library for checking SELinux status. |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | |
| 23 | #include "messages.h" |
| 24 | #include "mountlist.h" |
| 25 | #include "npl_selinux.h" |
| 26 | #include "sysfsparser.h" |
| 27 | #include "system.h" |
| 28 | |
| 29 | char *selinux_mnt = NULL; |
| 30 | |
| 31 | /* Return 1 if selinuxfs exists as a kernel filesystem, or 0 otherwise. */ |
| 32 | static int |
| 33 | selinuxfs_exists () |
| 34 | { |
| 35 | int exists; |
| 36 | |
| 37 | exists = file_system_type_exists (SELINUXFS, &selinux_mnt); |
| 38 | return (exists > 0) ? 1 : 0; |
| 39 | } |
| 40 | |
| 41 | /* Return 2 if we are running on a SELinux kernel in enhanced mode, |
| 42 | * 1 if SELinux is running in permissive mode, 0 otherwise. |
| 43 | */ |
| 44 | int |
| 45 | is_selinux_enabled (void) |
| 46 | { |
| 47 | int fs_exists = selinuxfs_exists (); |
| 48 | unsigned long long value; |
| 49 | |
| 50 | if (0 == fs_exists) |
| 51 | return 0; |
| 52 | if (0 == sysfsparser_path_exist ("%s/enforce", selinux_mnt)) |
| 53 | return 0; |
| 54 | |
| 55 | if ((sysfsparser_getvalue (&value, "%s/enforce", selinux_mnt) < 0)) |
| 56 | plugin_error (STATE_UNKNOWN, 0, |
| 57 | "cannot read the SELinux status from %s/enforce", |
| 58 | selinux_mnt); |
| 59 | |
| 60 | return value + 1; |
| 61 | } |