Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/util-host-info.c
Line
Count
Source
1
/* Copyright (C) 2014 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Eric Leblond <eric@regit.org>
22
 *
23
 * Get information on running host
24
 *
25
 */
26
27
#include "suricata-common.h"
28
#include "util-host-info.h"
29
#include "util-byte.h"
30
#include "util-debug.h"
31
32
#ifndef OS_WIN32
33
#include <sys/utsname.h>
34
35
0
#define VERSION_REGEX "^([0-9]+)\\.([0-9]+)"
36
37
int SCKernelVersionIsAtLeast(int major, int minor)
38
0
{
39
0
    struct utsname kuname;
40
0
    pcre2_code *version_regex;
41
0
    pcre2_match_data *version_regex_match;
42
0
    int en;
43
0
    int opts = 0;
44
0
    PCRE2_SIZE eo;
45
0
    int ret;
46
0
    int kmajor, kminor;
47
48
    /* get local version */
49
0
    if (uname(&kuname) != 0) {
50
0
        SCLogError("Invalid uname return: %s", strerror(errno));
51
0
        return 0;
52
0
    }
53
54
0
    SCLogDebug("Kernel release is '%s'", kuname.release);
55
56
0
    version_regex =
57
0
            pcre2_compile((PCRE2_SPTR8)VERSION_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
58
0
    if (version_regex == NULL) {
59
0
        PCRE2_UCHAR errbuffer[256];
60
0
        pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
61
0
        SCLogError("pcre2 compile of \"%s\" failed at "
62
0
                   "offset %d: %s",
63
0
                VERSION_REGEX, (int)eo, errbuffer);
64
0
        goto error;
65
0
    }
66
0
    version_regex_match = pcre2_match_data_create_from_pattern(version_regex, NULL);
67
68
0
    ret = pcre2_match(version_regex, (PCRE2_SPTR8)kuname.release, strlen(kuname.release), 0, 0,
69
0
            version_regex_match, NULL);
70
71
0
    if (ret < 0) {
72
0
        SCLogError("Version did not cut");
73
0
        goto error;
74
0
    }
75
76
0
    if (ret < 3) {
77
0
        SCLogError("Version major and minor not found (ret %d)", ret);
78
0
        goto error;
79
0
    }
80
81
0
    char majorstr[32];
82
0
    size_t pcre2len = sizeof(majorstr);
83
0
    ret = pcre2_substring_copy_bynumber(
84
0
            version_regex_match, 1, (PCRE2_UCHAR8 *)majorstr, &pcre2len);
85
0
    if (ret < 0) {
86
0
        SCLogError("pcre2_substring_copy_bynumber failed");
87
0
        goto error;
88
0
    }
89
90
0
    char minorstr[32];
91
0
    pcre2len = sizeof(majorstr);
92
0
    ret = pcre2_substring_copy_bynumber(
93
0
            version_regex_match, 2, (PCRE2_UCHAR8 *)minorstr, &pcre2len);
94
0
    if (ret < 0) {
95
0
        SCLogError("pcre2_substring_copy_bynumber failed");
96
0
        goto error;
97
0
    }
98
99
0
    if (StringParseInt32(&kmajor, 10, 0, (const char *)majorstr) < 0) {
100
0
        SCLogError("Invalid value for kmajor: '%s'", minorstr);
101
0
        goto error;
102
0
    }
103
0
    if (StringParseInt32(&kminor, 10, 0, (const char *)minorstr) < 0) {
104
0
        SCLogError("Invalid value for kminor: '%s'", minorstr);
105
0
        goto error;
106
0
    }
107
108
0
    pcre2_match_data_free(version_regex_match);
109
0
    pcre2_code_free(version_regex);
110
111
0
    if (kmajor > major)
112
0
        return 1;
113
0
    if (kmajor == major && kminor >= minor)
114
0
        return 1;
115
0
error:
116
0
    return 0;
117
0
}
118
119
#else /* OS_WIN32 */
120
121
int SCKernelVersionIsAtLeast(int major, int minor)
122
{
123
    SCLogError("OS compare is not supported on Windows");
124
    return 0;
125
}
126
127
#endif /* OS_WIN32 */