Skip to content

Commit f6fbafe

Browse files
authored
Merge pull request #1467 from evgenyz/covscan-fixes
Covscan fixes
2 parents dbb2d8f + 7bccc09 commit f6fbafe

File tree

6 files changed

+80
-56
lines changed

6 files changed

+80
-56
lines changed

src/OVAL/probes/fsdev.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static int is_local_fs(struct mntent *ment)
9797
return 0;
9898
}
9999

100+
if (ment->mnt_fsname == NULL) {
101+
return 0;
102+
}
103+
100104
s = ment->mnt_fsname;
101105
/* If the fsname begins with "//", it is probably CIFS. */
102106
if (s[0] == '/' && s[1] == '/')

src/XCCDF_POLICY/xccdf_policy_remediate.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ static inline int _xccdf_fix_decode_xml(struct xccdf_fix *fix, char **result)
380380
#if defined(unix) || defined(__unix__) || defined(__unix)
381381
static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix)
382382
{
383-
if (fix == NULL || rr == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
383+
if (rr == NULL) {
384+
return 1;
385+
}
386+
387+
if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
384388
_rule_add_info_message(rr, "No fix available.");
385389
return 1;
386390
}
@@ -481,7 +485,11 @@ static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_
481485
#else
482486
static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix)
483487
{
484-
if (fix == NULL || rr == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
488+
if (rr == NULL) {
489+
return 1;
490+
}
491+
492+
if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
485493
_rule_add_info_message(rr, "No fix available.");
486494
return 1;
487495
} else {

utils/oscap-chroot

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ function die()
2525
exit 1
2626
}
2727

28+
function invalid()
29+
{
30+
echo -e "$*\n" >&2
31+
usage
32+
exit 1
33+
}
34+
2835
function usage()
2936
{
3037
echo "oscap-chroot -- Tool for offline SCAP evaluation of filesystems mounted in arbitrary paths."
@@ -74,26 +81,23 @@ function usage()
7481
}
7582

7683
if [ $# -lt 1 ]; then
77-
echo "No arguments provided."
78-
usage
79-
die
84+
invalid "No arguments provided."
8085
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
8186
usage
82-
die
87+
exit 0
8388
elif [ "$#" -gt 1 ]; then
8489
true
8590
else
86-
echo "Invalid arguments provided."
87-
usage
88-
die
91+
invalid "Invalid arguments provided."
8992
fi
9093

9194
# Learn more at https://www.redhat.com/archives/open-scap-list/2013-July/msg00000.html
9295
export OSCAP_PROBE_ROOT
93-
OSCAP_PROBE_ROOT="$(cd "$1"; pwd)"
96+
OSCAP_PROBE_ROOT="$(cd "$1" && pwd)" || die "Invalid CHROOT_PATH argument."
9497
export OSCAP_EVALUATION_TARGET="chroot://$OSCAP_PROBE_ROOT"
9598
shift 1
9699

97100
oscap "$@"
98101
EXIT_CODE=$?
102+
99103
exit $EXIT_CODE

utils/oscap-podman

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
# License along with this library; if not, write to the Free Software
1717
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818

19-
2019
function die()
2120
{
2221
echo "$*" >&2
2322
exit 1
2423
}
2524

25+
function invalid()
26+
{
27+
echo -e "$*\n" >&2
28+
usage
29+
exit 1
30+
}
31+
2632
function usage()
2733
{
2834
echo "oscap-podman -- Tool for SCAP evaluation of Podman images and containers."
@@ -39,30 +45,24 @@ function usage()
3945
OSCAP_BINARY=oscap
4046

4147
if [ $# -lt 1 ]; then
42-
echo "No arguments provided."
43-
usage
44-
die
48+
invalid "No arguments provided."
4549
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
4650
usage
47-
die
51+
exit 0
4852
elif [[ "$1" == --oscap=* ]] && [ $# -gt 2 ]; then
4953
OSCAP_BINARY=${1#"--oscap="}
5054
shift
5155
elif [ "$#" -gt 1 ]; then
5256
true
5357
else
54-
echo "Invalid arguments provided."
55-
usage
56-
die
58+
invalid "Invalid arguments provided."
5759
fi
5860

5961
if [ $(id -u) -ne 0 ]; then
60-
echo "This script cannot run in rootless mode." >&2
61-
die
62+
die "This script cannot run in rootless mode."
6263
fi
6364
if grep -q "\-\-remediate" <<< "$@"; then
64-
echo "This script does not support '--remediate' option." >&2
65-
die
65+
die "This script does not support '--remediate' option."
6666
fi
6767

6868
IMAGE_NAME=$(podman image exists "$1" \
@@ -72,29 +72,27 @@ CONTAINER_NAME=$(podman container exists "$1" \
7272

7373
if [ -n "$IMAGE_NAME" ] && [ -n "$CONTAINER_NAME" ]; then
7474
echo "Ambiguous target, container image and container with the same name detected: '$1'." >&2
75-
echo "Please rather use an unique ID to specify the target of the scan." >&2
76-
die
75+
die "Please rather use an unique ID to specify the target of the scan."
7776
fi
7877

7978
# Check if the target of scan is image or container.
8079
CLEANUP=0
8180
if [ -n "$IMAGE_NAME" ]; then
82-
ID=$(podman create $1) || die
81+
ID=$(podman create $1) || die "Unable to create a container."
8382
TARGET="podman-image://$IMAGE_NAME"
8483
CLEANUP=1
8584
elif [ -n "$CONTAINER_NAME" ]; then
8685
# If the target was not found in images we suppose it is a container.
8786
ID=$1
8887
TARGET="podman-container://$CONTAINER_NAME"
8988
else
90-
echo "Target of the scan not found: '$1'." >&2
91-
die
89+
die "Target of the scan not found: '$1'."
9290
fi
9391

9492
# podman init creates required files such as: /run/.containerenv - we don't care about output and exit code
9593
podman init $ID &> /dev/null || true
9694

97-
DIR=$(podman mount $ID) || die
95+
DIR=$(podman mount $ID) || die "Failed to mount."
9896

9997
if [ ! -f "$DIR/run/.containerenv" ]; then
10098
# ubi8-init image does not create .containerenv when running podman init, but we need to make sure that the file is there
@@ -105,14 +103,16 @@ for VAR in `podman inspect $ID --format '{{join .Config.Env " "}}'`; do
105103
eval "export OSCAP_OFFLINE_$VAR"
106104
done
107105

108-
export OSCAP_PROBE_ROOT="$(cd "$DIR"; pwd)"
106+
export OSCAP_PROBE_ROOT
107+
OSCAP_PROBE_ROOT="$(cd "$DIR" && pwd)" || die "Unable to change current directory to OSCAP_PROBE_ROOT (DIR)."
109108
export OSCAP_EVALUATION_TARGET="$TARGET"
110109
shift 1
111110

112111
$OSCAP_BINARY "$@"
113112
EXIT_CODE=$?
114-
podman umount $ID > /dev/null || die
113+
114+
podman umount $ID > /dev/null || die "Failed to unmount."
115115
if [ $CLEANUP -eq 1 ]; then
116-
podman rm $ID > /dev/null || die
116+
podman rm $ID > /dev/null || die "Failed to clean up."
117117
fi
118118
exit $EXIT_CODE

utils/oscap-ssh

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ function die()
2222
exit 1
2323
}
2424

25-
hash ssh 2> /dev/null || die "Cannot find ssh, please install the OpenSSH client."
26-
hash scp 2> /dev/null || die "Cannot find scp, please install the OpenSSH client."
27-
hash mktemp 2> /dev/null || die "Cannot find mktemp, please install coreutils."
25+
function invalid()
26+
{
27+
echo -e "$*\n" >&2
28+
usage
29+
exit 1
30+
}
2831

2932
function usage()
3033
{
@@ -87,10 +90,6 @@ function usage()
8790
echo "See \`man oscap\` to learn more about semantics of these options."
8891
}
8992

90-
OSCAP_SUDO=""
91-
# SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
92-
SSH_TTY_ALLOCATION_OPTION=""
93-
9493
# $1, $2, ... SSH options (pass them as separate arguments)
9594
function ssh_execute_with_options {
9695
ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS "$@" -p "$SSH_PORT" "$SSH_HOST"
@@ -118,32 +117,28 @@ function scp_retreive_from_temp_dir {
118117
# Returns: String, where individual command components are double-quoted, so they are not interpreted by the shell.
119118
# For example, an array ('-p' '(all)') will be transformed to "\"-p\" \"(all)\"", so after the shell expansion, it will end up as "-p" "(all)".
120119
function command_array_to_string {
121-
eval "printf '\"%s\" ' \"\${$1[@]}\""
120+
eval "printf '\"%s\" ' \"\${$1[@]}\""
122121
}
123122

124123
function first_argument_is_sudo {
125-
[ "$1" == "sudo" ] || [ "$1" == "--sudo" ]
126-
return $?
124+
[ "$1" == "sudo" ] || [ "$1" == "--sudo" ]
125+
return $?
127126
}
128127

129128
function sanity_check_arguments {
130129
if [ $# -lt 1 ]; then
131-
echo "No arguments provided."
132-
usage
133-
die
130+
invalid "No arguments provided."
134131
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
135132
usage
136-
die
133+
exit 0
137134
elif first_argument_is_sudo "$@"; then
138135
OSCAP_SUDO="sudo"
139136
# force pseudo-tty allocation so that users can type their password if necessary
140137
SSH_TTY_ALLOCATION_OPTION="-t"
141138
shift
142139
fi
143140
if [ $# -lt 2 ]; then
144-
echo "Missing ssh host and ssh port."
145-
usage
146-
die
141+
invalid "Missing ssh host and ssh port."
147142
fi
148143
}
149144

@@ -165,6 +160,16 @@ function check_oscap_arguments {
165160
fi
166161
}
167162

163+
164+
hash ssh 2> /dev/null || die "Cannot find ssh, please install the OpenSSH client."
165+
hash scp 2> /dev/null || die "Cannot find scp, please install the OpenSSH client."
166+
hash mktemp 2> /dev/null || die "Cannot find mktemp, please install coreutils."
167+
168+
169+
OSCAP_SUDO=""
170+
# SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
171+
SSH_TTY_ALLOCATION_OPTION=""
172+
168173
sanity_check_arguments "$@"
169174
first_argument_is_sudo "$@" && shift
170175

utils/oscap-vm

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function die()
2222
exit 1
2323
}
2424

25+
function invalid()
26+
{
27+
echo -e "$*\n" >&2
28+
usage
29+
exit 1
30+
}
31+
2532
function usage()
2633
{
2734
echo "oscap-vm -- Tool for offline SCAP evaluation of virtual machines."
@@ -76,12 +83,10 @@ function usage()
7683
OSCAP_BINARY=oscap
7784

7885
if [ $# -lt 1 ]; then
79-
echo "No arguments provided."
80-
usage
81-
die
86+
invalid "No arguments provided."
8287
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
8388
usage
84-
die
89+
exit 0
8590
elif [[ "$1" == --oscap=* ]] && [ $# -gt 3 ]; then
8691
OSCAP_BINARY=${1#"--oscap="}
8792
shift
@@ -90,9 +95,7 @@ elif [ "$1" == "image" ] && [ $# -gt 2 ]; then
9095
elif [ "$1" == "domain" ] && [ $# -gt 2 ]; then
9196
true
9297
else
93-
echo "Invalid arguments provided."
94-
usage
95-
die
98+
invalid "Invalid arguments provided."
9699
fi
97100

98101
hash guestmount 2> /dev/null || die "Cannot find guestmount, please install libguestfs utilities."
@@ -128,7 +131,7 @@ fi
128131

129132
# Learn more at https://www.redhat.com/archives/open-scap-list/2013-July/msg00000.html
130133
export OSCAP_PROBE_ROOT
131-
OSCAP_PROBE_ROOT="$(cd "$MOUNTPOINT"; pwd)"
134+
OSCAP_PROBE_ROOT="$(cd "$MOUNTPOINT" && pwd)" || die "Unable to change current directory to OSCAP_PROBE_ROOT (MOUNTPOINT)."
132135
export OSCAP_EVALUATION_TARGET="oscap-vm $1 $2"
133136
shift 2
134137

0 commit comments

Comments
 (0)