Skip to content

Commit 0862f59

Browse files
authored
Merge pull request #2007 from evgenyz/fix-coverity-1.3.8
Fix coverity 1.3.8
2 parents 1347831 + 6256a25 commit 0862f59

File tree

8 files changed

+256
-183
lines changed

8 files changed

+256
-183
lines changed

src/OVAL/probes/unix/linux/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ endif()
9292
if(OPENSCAP_PROBE_LINUX_SYSTEMDUNITDEPENDENCY OR OPENSCAP_PROBE_LINUX_SYSTEMDUNITPROPERTY)
9393
list(APPEND LINUX_PROBES_SOURCES
9494
"systemdshared.h"
95+
"oval_dbus.c"
96+
"oval_dbus.h"
9597
)
9698
list(APPEND LINUX_PROBES_INCLUDE_DIRECTORIES
9799
${DBUS_INCLUDE_DIRS}
@@ -113,10 +115,17 @@ if(OPENSCAP_PROBE_LINUX_SYSTEMDUNITPROPERTY)
113115
endif()
114116

115117
if(OPENSCAP_PROBE_LINUX_FWUPDSECURITYATTR)
118+
list(APPEND LINUX_PROBES_SOURCES
119+
"oval_dbus.c"
120+
"oval_dbus.h"
121+
)
116122
list(APPEND LINUX_PROBES_SOURCES
117123
"fwupdsecattr_probe.c"
118124
"fwupdsecattr_probe.h"
119125
)
126+
list(APPEND LINUX_PROBES_INCLUDE_DIRECTORIES
127+
${DBUS_INCLUDE_DIRS}
128+
)
120129
endif()
121130

122131

src/OVAL/probes/unix/linux/fwupdsecattr_probe.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
#include "probe/entcmp.h"
6262
#include "common/debug_priv.h"
6363

64+
#include "oval_dbus.h"
6465
#include "fwupdsecattr_probe.h"
65-
#include "systemdshared.h"
6666

6767

6868
static struct cachehed hsi_result_cache;
@@ -206,24 +206,26 @@ static int get_all_security_attributes(DBusConnection *conn, void(*callback)(cha
206206

207207
switch (arg_type) {
208208
case DBUS_TYPE_UINT32:
209-
if(strncmp(property_name, "HsiResult", strlen("HsiResult")) == 0) {
209+
if(!strncmp(property_name, "HsiResult", strlen("HsiResult"))) {
210210
_DBusBasicValue hsiresult_value;
211211
dbus_message_iter_get_basic(&value_variant, &hsiresult_value);
212212
hsi_flags = hsiresult_value.u32;
213213
}
214214
break;
215215
case DBUS_TYPE_STRING:
216216
if(!strncmp(property_name, "AppstreamId", strlen("AppstreamId"))) {
217-
appstream_name = dbus_value_to_string(&value_variant);
217+
free(appstream_name);
218+
appstream_name = oval_dbus_value_to_string(&value_variant);
218219
dD("Element string: %s", appstream_name);
219220
}
220221
break;
221222
}
222223
free(property_name);
223224
} while (dbus_message_iter_next(&array_entry));
224225
callback(appstream_name, hsi_flags);
225-
}
226-
while (dbus_message_iter_next(&property_iter));
226+
free(appstream_name);
227+
appstream_name = NULL;
228+
} while (dbus_message_iter_next(&property_iter));
227229

228230
dbus_message_unref(msg); msg = NULL;
229231
ret = 0;
@@ -315,27 +317,27 @@ int fwupdsecattr_probe_main(probe_ctx *ctx, void *arg)
315317
DBusConnection *dbus_conn;
316318

317319
dbus_error_init(&dbus_error);
318-
dbus_conn = connect_dbus();
320+
dbus_conn = oval_connect_dbus();
319321

320322
if (dbus_conn == NULL) {
321323
dbus_error_free(&dbus_error);
322324
SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "D-Bus connection failed, could not identify fwupd.");
323325
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
324326
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
325327
SEXP_free(msg);
326-
return 0;
328+
goto exit;
327329
}
328330

329331
int res = get_all_security_attributes(dbus_conn, hsicache_callback, NULL);
330-
disconnect_dbus(dbus_conn);
332+
oval_disconnect_dbus(dbus_conn);
331333

332334
if (res) {
333335
dbus_error_free(&dbus_error);
334336
SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "The fwupd service is not properly installed or configured.");
335337
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
336338
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
337339
SEXP_free(msg);
338-
return 0;
340+
goto exit;
339341
}
340342
}
341343

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2023 Red Hat Inc., Durham, North Carolina.
3+
* All Rights Reserved.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*
19+
* Authors:
20+
* Evgenii Kolesnikov <[email protected]>
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
#include <config.h>
25+
#endif
26+
27+
#include <limits.h>
28+
#include "common/util.h"
29+
#include "oscap_helpers.h"
30+
#include "common/debug_priv.h"
31+
#include "oval_dbus.h"
32+
33+
34+
char *oval_dbus_value_to_string(DBusMessageIter *iter)
35+
{
36+
const int arg_type = dbus_message_iter_get_arg_type(iter);
37+
if (dbus_type_is_basic(arg_type)) {
38+
_DBusBasicValue value;
39+
dbus_message_iter_get_basic(iter, &value);
40+
41+
switch (arg_type)
42+
{
43+
case DBUS_TYPE_BYTE:
44+
return oscap_sprintf("%c", value.byt);
45+
46+
case DBUS_TYPE_BOOLEAN:
47+
return oscap_strdup(value.bool_val ? "true" : "false");
48+
49+
case DBUS_TYPE_INT16:
50+
return oscap_sprintf("%i", value.i16);
51+
52+
case DBUS_TYPE_UINT16:
53+
return oscap_sprintf("%u", value.u16);
54+
55+
case DBUS_TYPE_INT32:
56+
return oscap_sprintf("%i", value.i32);
57+
58+
case DBUS_TYPE_UINT32:
59+
return oscap_sprintf("%u", value.u32);
60+
61+
#ifdef DBUS_HAVE_INT64
62+
case DBUS_TYPE_INT64:
63+
return oscap_sprintf("%li", value.i64);
64+
65+
case DBUS_TYPE_UINT64:
66+
return oscap_sprintf("%lu", value.u64);
67+
#endif
68+
69+
case DBUS_TYPE_DOUBLE:
70+
return oscap_sprintf("%g", value.dbl);
71+
72+
case DBUS_TYPE_STRING:
73+
case DBUS_TYPE_OBJECT_PATH:
74+
case DBUS_TYPE_SIGNATURE:
75+
return oscap_strdup(value.str);
76+
77+
// We skip non-basic types for now
78+
//case DBUS_TYPE_ARRAY:
79+
//case DBUS_TYPE_STRUCT:
80+
//case DBUS_TYPE_DICT_ENTRY:
81+
//case DBUS_TYPE_VARIANT:
82+
//case DBUS_TYPE_UNIX_FD:
83+
// return oscap_sprintf("%i", value.fd);
84+
85+
default:
86+
dD("Encountered unknown D-Bus basic type: %d!", arg_type);
87+
return oscap_strdup("error, unknown basic type!");
88+
}
89+
} else if (arg_type == DBUS_TYPE_ARRAY) {
90+
DBusMessageIter array;
91+
dbus_message_iter_recurse(iter, &array);
92+
93+
char *ret = NULL;
94+
do {
95+
char *element = oval_dbus_value_to_string(&array);
96+
97+
if (element == NULL)
98+
continue;
99+
100+
char *old_ret = ret;
101+
if (old_ret == NULL)
102+
ret = oscap_sprintf("%s", element);
103+
else
104+
ret = oscap_sprintf("%s, %s", old_ret, element);
105+
106+
free(old_ret);
107+
free(element);
108+
}
109+
while (dbus_message_iter_next(&array));
110+
111+
return ret;
112+
}
113+
114+
return NULL;
115+
}
116+
117+
DBusConnection *oval_connect_dbus(void)
118+
{
119+
DBusConnection *conn = NULL;
120+
121+
DBusError err;
122+
dbus_error_init(&err);
123+
124+
const char *prefix = getenv("OSCAP_PROBE_ROOT");
125+
if (prefix != NULL) {
126+
char dbus_address[PATH_MAX] = {0};
127+
snprintf(dbus_address, PATH_MAX, "unix:path=%s/run/dbus/system_bus_socket", prefix);
128+
setenv("DBUS_SYSTEM_BUS_ADDRESS", dbus_address, 0);
129+
/* We won't overwrite DBUS_SYSTEM_BUS_ADDRESS so that
130+
* user could have a way to define some non-standard system bus socket location */
131+
}
132+
133+
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
134+
if (dbus_error_is_set(&err)) {
135+
dD("Failed to get DBUS_BUS_SYSTEM connection - %s", err.message);
136+
goto cleanup;
137+
}
138+
if (conn == NULL) {
139+
dD("DBusConnection == NULL!");
140+
goto cleanup;
141+
}
142+
143+
dbus_bus_register(conn, &err);
144+
if (dbus_error_is_set(&err)) {
145+
dD("Failed to register on dbus - %s", err.message);
146+
goto cleanup;
147+
}
148+
149+
cleanup:
150+
dbus_error_free(&err);
151+
152+
return conn;
153+
}
154+
155+
void oval_disconnect_dbus(DBusConnection *conn)
156+
{
157+
// NOOP
158+
159+
// Connections retrieved via dbus_bus_get shall not be destroyed,
160+
// these connections are shared.
161+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023 Red Hat Inc., Durham, North Carolina.
3+
* All Rights Reserved.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*
19+
* Authors:
20+
* Evgenii Kolesnikov <[email protected]>
21+
*/
22+
23+
#ifndef OPENSCAP_OVAL_DBUS_H_
24+
#define OPENSCAP_OVAL_DBUS_H_
25+
26+
#include <dbus/dbus.h>
27+
28+
29+
// Old versions of libdbus API don't have DBusBasicValue and DBus8ByteStruct
30+
// as a public typedefs.
31+
// These two typedefs were copied from libdbus 1.8 branch, see
32+
// http://cgit.freedesktop.org/dbus/dbus/tree/dbus/dbus-types.h?h=dbus-1.8#n137
33+
typedef struct
34+
{
35+
dbus_uint32_t first32;
36+
dbus_uint32_t second32;
37+
} _DBus8ByteStruct;
38+
39+
typedef union
40+
{
41+
unsigned char bytes[8]; /**< as 8 individual bytes */
42+
dbus_int16_t i16; /**< as int16 */
43+
dbus_uint16_t u16; /**< as int16 */
44+
dbus_int32_t i32; /**< as int32 */
45+
dbus_uint32_t u32; /**< as int32 */
46+
dbus_bool_t bool_val; /**< as boolean */
47+
#ifdef DBUS_HAVE_INT64
48+
dbus_int64_t i64; /**< as int64 */
49+
dbus_uint64_t u64; /**< as int64 */
50+
#endif
51+
_DBus8ByteStruct eight; /**< as 8-byte struct */
52+
double dbl; /**< as double */
53+
unsigned char byt; /**< as byte */
54+
char *str; /**< as char* (string, object path or signature) */
55+
int fd; /**< as Unix file descriptor */
56+
} _DBusBasicValue;
57+
58+
59+
char *oval_dbus_value_to_string(DBusMessageIter *iter);
60+
61+
DBusConnection *oval_connect_dbus(void);
62+
63+
void oval_disconnect_dbus(DBusConnection *conn);
64+
65+
#endif

0 commit comments

Comments
 (0)