CVE-2014-0593 (published 2018-06-08T17:29Z)

The set_version script as shipped with obs-service-set_version is a source validator for the Open Build Service (OBS). In versions prior to 0.5.3-1.1 this script did not properly sanitize the input provided by the user, allowing for code execution on the executing server.

Severity: HIGH
CVSS v2 base score: 10.0 (AV:N/AC:L/Au:N/C:C/I:C/A:C)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-20
cwe_name: Improper Input Validation
description: The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
extended_description: 
	   Input validation is a frequently-used technique
	   for checking potentially dangerous inputs in order to
	   ensure that the inputs are safe for processing within the
	   code, or when communicating with other components.  When
	   software does not validate input properly, an attacker is
	   able to craft the input in a form that is not expected by
	   the rest of the application. This will lead to parts of the
	   system receiving unintended input, which may result in
	   altered control flow, arbitrary control of a resource, or
	   arbitrary code execution.
	   Input validation is not the only technique for
	   processing input, however.  Other techniques attempt to
	   transform potentially-dangerous input into something safe, such
	   as filtering (CWE-790) - which attempts to remove dangerous
	   inputs - or encoding/escaping (CWE-116), which attempts to
	   ensure that the input is not misinterpreted when it is included
	   in output to another component. Other techniques exist as well
	   (see CWE-138 for more examples.)
	   Input validation can be applied to:
	   
	     raw data - strings, numbers, parameters, file contents, etc.
	     metadata - information about the raw data, such as headers or size
	   
	   Data can be simple or structured.  Structured data
	   can be composed of many nested layers, composed of
	   combinations of metadata and raw data, with other simple or
	   structured data.
	   Many properties of raw data or metadata may need
	   to be validated upon entry into the code, such
	   as:
	   
	     specified quantities such as size, length, frequency, price, rate, number of operations, time, etc.
	     implied or derived quantities, such as the actual size of a file instead of a specified size
	     indexes, offsets, or positions into more complex data structures
	     symbolic keys or other elements into hash tables, associative arrays, etc.
	     well-formedness, i.e. syntactic correctness - compliance with expected syntax 
	     lexical token correctness - compliance with rules for what is treated as a token
	     specified or derived type - the actual type of the input (or what the input appears to be)
	     consistency - between individual data elements, between raw data and metadata, between references, etc.
	     conformance to domain-specific rules, e.g. business logic 
	     equivalence - ensuring that equivalent inputs are treated the same
	     authenticity, ownership, or other attestations about the input, e.g. a cryptographic signature to prove the source of the data
	   
	   Implied or derived properties of data must often
	   be calculated or inferred by the code itself.  Errors in
	   deriving properties may be considered a contributing factor
	   to improper input validation.
	   
	   Note that "input validation" has very different
	   meanings to different people, or within different
	   classification schemes.  Caution must be used when
	   referencing this CWE entry or mapping to it.  For example,
	   some weaknesses might involve inadvertently giving control
	   to an attacker over an input when they should not be able
	   to provide an input at all, but sometimes this is referred
	   to as input validation.
	   Finally, it is important to emphasize that the
	   distinctions between input validation and output escaping
	   are often blurred, and developers must be careful to
	   understand the difference, including how input validation
	   is not always sufficient to prevent vulnerabilities,
	   especially when less stringent data types must be
	   supported, such as free-form text. Consider a SQL injection
	   scenario in which a person's last name is inserted into a
	   query. The name "O'Reilly" would likely pass the validation
	   step since it is a common last name in the English
	   language. However, this valid name cannot be directly
	   inserted into the database because it contains the "'"
	   apostrophe character, which would need to be escaped or
	   otherwise transformed. In this case, removing the
	   apostrophe might reduce the risk of SQL injection, but it
	   would produce incorrect behavior because the wrong name
	   would be recorded.
       
         

Fix information:
repo_url: https://github.com/openSUSE/obs-service-set_version
commit_details: repo_url: https://github.com/openSUSE/obs-service-set_version
author: Adrian Schröter
author_date: 2014-03-06 12:00:23+01:00
msg: - replace bash script with a more secure perl version
repository: repo_url: https://github.com/openSUSE/obs-service-set_version
repo_name: openSUSE/obs-service-set_version
description: An OBS source service: Update spec file version
repo_language: Python
owner: openSUSE
file_changes: filename: set_version
change_type: ModificationType.MODIFY
diff: @@ -1,7 +1,7 @@
-#!/bin/bash
+#!/usr/bin/perl
 
-# A simple script to update spec or dsc file
-# very, very simple. I am happy about patches which handles multiple files with different version numbers
+
+# A simple script to update version number in spec, dsc or arch linux files
 #
 # (C) 2010 by Adrian Schröter <adrian@suse.de>
 #  
@@ -12,105 +12,149 @@
 # See http://www.gnu.org/licenses/gpl-2.0.html for full license text.  
 
 
-# defaults
-MYVERSION=""
-FILES=""
-
-while test $# -gt 0; do
-  case $1 in
-    *-version)
-      MYVERSION="$2"
-      shift
-    ;;
-    *-file)
-      FILES="$FILES ${2##*/}"
-      shift
-    ;;
-    *-basename)
-      BASENAME="$2"
-      shift
-    ;;
-    *-outdir)
-      MYOUTDIR="$2"
-      shift
-    ;;
-    *)
-      echo Unknown parameter $1.
-      echo 'Usage: set_version --version $VERSION --file $FILE --basename $BASENAME --outdir $OUT'
-      exit 1
-    ;;
-  esac
-  shift
-done
-
-get_version_from_file () {
-  # Search for tarball filenames including a version
-  for ending in "tar.*" "tgz$" "tbz2$" "zip$" ; do
-    if [ -z "$MYVERSION" ]; then
-      MYVERSION=`ls -1t | sed -n "s,^$BASENAME.*[-_]\([0123456789].*\).${ending},\1,p" | head -n 1`
-    else
-      break;
-    fi
-  done
-  # Search for a version in the root-directory name of the content's file list:
-  for gz_ending in "tar.gz" "tgz" "tar.bz2" "tbz2" ; do
-    if [ -z "$MYVERSION" ]; then
-      MYVERSION=`tar tf $BASENAME*.${gz_ending} | sed -e 's,/.*,,' | uniq | sed -n "s,$BASENAME.*[-_]\(.*\),\1,p"`
-    else
-      break;
-    fi
-  done
-  # Take version number (and optional revision) from Debian changelog
-  if [ -z "$MYVERSION" ]; then
-    MYVERSION=`head -n 1 *debian.changelog 2>/dev/null | sed -ne 's/.*(\(.*\)).*/\1/p'`
-  fi
-  if [ -z "$MYVERSION" ]; then
-    echo "ERROR: no version is given and can't get detected automatically"
-    exit 1
-  fi
-
-  # remove trailing ".orig", used by dpkg sources
-  MYVERSION=`sed s/.orig$// <<<"$MYVERSION"`
+
+my $version;
+my $basename="";
+my $outdir;
+my @files;
+
+use strict;
+use Data::Dumper;
+use File::Basename;
+
+sub usage()
+{
+  print<<END
+
+  Open Build Service source service "set_version"
+
+  Used to update build description files with a detected or given version number.
+
+  Required:
+
+  --output:   mediaset, build only for the given mediaset, identify by name
+
+  Options:
+
+END
+;
+  exit;
+}
+
+while (@ARGV) {
+  usage() if $ARGV[0] eq '--help';
+  if ($ARGV[0] eq '--outdir') {
+    shift @ARGV;
+    $outdir = shift @ARGV;
+    next;
+  } elsif ($ARGV[0] eq '--version') {
+    shift @ARGV;
+    $version = shift @ARGV;
+    next;
+  } elsif ($ARGV[0] eq '--basename') {
+    # this is actually more a prefix
+    shift @ARGV;
+    $basename = shift @ARGV;
+    next;
+  } elsif ($ARGV[0] eq '--file') {
+    shift @ARGV;
+    push @files, shift @ARGV;
+    next;
+  } else {
+    die("Unknown argument $ARGV[0]!");
+  }
+  last;
 }
 
-write_files () {
-  if [ -z "$FILES" ]; then
-    FILES="*.spec *.dsc"
-  fi
-  if [ -z "$MYOUTDIR" ]; then
-    echo "ERROR: no output directory is given via --outdir parameter!"
-    exit 1
-  fi
-
-  for i in $FILES; do
-    FILE=`ls -1 $i 2>/dev/null`
-    [ -e "$FILE" ] || continue
-
-    sed "0,/^Version:\(\s*\)[^%]*/s//Version:\1$MYVERSION/" "$FILE" > "$MYOUTDIR/$FILE" || exit 1
-    if [ "${FILE%.spec}" != "$FILE" ]; then
-      # set release back to zero after version upgrade, will be increased by OBS during build
-      # also keep macros in release in case of fedora/mandriva
-      sed -r -i "s,^Release:(\s*)[^%]*,Release:\10," "$MYOUTDIR/$FILE" || exit 1
-   fi
-
-    if [ "${FILE#_service:}" != "$FILE" ]; then
-      # we can remove service files, no need to store them twice
-      rm -f "$FILE"
-    fi
-  done
-
-  if [ -e PKGBUILD ]; then
-    FILE=`ls -1 _service:*[-_]"${MYVERSION}"* | head -n 1`
-    [ -z "$FILE" ] && FILE=`ls -1 *[-_]"${MYVERSION}"* | head -n 1`
-    MD5=`md5sum "$FILE" | head -n 1 | cut -d\  -f 1`
-
-    sed "0,/^pkgver=\(\s*\)[^%]*/s//pkgver=\1$MYVERSION/" PKGBUILD > "$MYOUTDIR/PKGBUILD" || exit 1
-    sed -r -i "s,^pkgrel=.*,pkgrel=0," "$MYOUTDIR/PKGBUILD" || exit 1
-    sed -r -i "s,^md5sums=.*,md5sums=('$MD5')," "$MYOUTDIR/PKGBUILD" || exit 1
-  fi
+usage() unless $outdir;
+
+# get local file list
+local *D;
+opendir(D, ".") || return ();
+my @srcfiles = grep {$_ ne '.' && $_ ne '..'} readdir(D);
+closedir D;
+
+# Detect version based on file names
+unless ($version) {
+  my @binsufs = qw{tar tar.gz tgz tar.bz2 tbz2 tar.xz zip};
+  my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);
+  for my $name (@srcfiles) {
+    if ($name =~ /^$basename.*[-_]([\d].*).(?:$binsufsre)$/) {
+       $version=$1;
+       last;
+    }
+  }
 }
 
-get_version_from_file
-write_files
+# to replace tags in .spec and .dsc files
+sub replace_tag {
+  my ($filename, $tag, $string) = @_;
+  local *R;
+  local *W;
+  # read, try first an modified version in outdir
+  if (!open(R, '<', "$outdir/$filename")) {
+    if (!open(R, '<', $filename)) {
+      die("unable to read $filename: $!\n");
+      return undef;
+    }
+  }
+  if (!open(W, '>', "$outdir/.$filename")) {
+    die("unable to write .$filename: $!\n");
+    return undef;
+  }
+  my $line;
+  while (defined($line = <R>)) {
+    $line =~ s/\n$//;
+    if ( $filename =~ /PKGBUILD$/ ) {
+      $line =~ s/^$tag=.*/$tag=$string/;
+    } else {
+      # keep inline macros for rpm
+      $line =~ s/^$tag:(\s*)[^%]*/$tag:$1$string/;
+    }
+    print W "$line\n";
+  }
+  close R;
+  close W;
 
-exit 0
+  rename("$outdir/.$filename", "$outdir/$filename") || die("rename failed");
+}
+
+# handle rpm spec and debian dsc files
+@files = grep {$_ =~ /.(spec|dsc)$/} @srcfiles if @files <= 0;
+for my $file (@files) {
+  replace_tag($file, 'Version', $version);
+  replace_tag($file, 'Release', "0");
+}
+
+use Digest::MD5;
+
+# handle arch linux PKGBUILD files
+if ( -e "PKGBUILD" ) {
+  # find md5sum of tar ball
+  my $md5sum;
+  my $tarfile;
+  for my $file (@srcfiles) {
+    if ( $file =~ /^_service:.*[-_]$version.*/ ) {
+      $tarfile = $file;
+      last;
+    }
+  }
+  unless($tarfile) {
+    for my $file (@srcfiles) {
+      if ( $file =~ /.*[-_]$version.*/ ) {
+        $tarfile = $file;
+        last;
+      }
+    }
+  }
+
+  open(FILE, $tarfile) || die "Unable to find source file to calculate md5sum\n";
+  my $ctx = Digest::MD5->new;
+  $ctx->addfile(*FILE);
+  $md5sum = $ctx->hexdigest;
+  close(FILE);
+
+  die ("Failed to calculate md5sum") unless $md5sum;
+  replace_tag("PKGBUILD", "pkgrel", "0");
+  replace_tag("PKGBUILD", "md5sums", "('".$md5sum."')");
+}

programming_language: Perl
filename: set_version.old.do_not_use
change_type: ModificationType.ADD
diff: @@ -0,0 +1,116 @@
+#!/bin/bash
+
+# A simple script to update spec or dsc file
+# very, very simple. I am happy about patches which handles multiple files with different version numbers
+#
+# (C) 2010 by Adrian Schröter <adrian@suse.de>
+#  
+# This program is free software; you can redistribute it and/or  
+# modify it under the terms of the GNU General Public License  
+# as published by the Free Software Foundation; either version 2  
+# of the License, or (at your option) any later version.  
+# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.  
+
+
+# defaults
+MYVERSION=""
+FILES=""
+
+while test $# -gt 0; do
+  case $1 in
+    *-version)
+      MYVERSION="$2"
+      shift
+    ;;
+    *-file)
+      FILES="$FILES ${2##*/}"
+      shift
+    ;;
+    *-basename)
+      BASENAME="$2"
+      shift
+    ;;
+    *-outdir)
+      MYOUTDIR="$2"
+      shift
+    ;;
+    *)
+      echo Unknown parameter $1.
+      echo 'Usage: set_version --version $VERSION --file $FILE --basename $BASENAME --outdir $OUT'
+      exit 1
+    ;;
+  esac
+  shift
+done
+
+get_version_from_file () {
+  # Search for tarball filenames including a version
+  for ending in "tar.*" "tgz$" "tbz2$" "zip$" ; do
+    if [ -z "$MYVERSION" ]; then
+      MYVERSION=`ls -1t | sed -n "s,^$BASENAME.*[-_]\([0123456789].*\).${ending},\1,p" | head -n 1`
+    else
+      break;
+    fi
+  done
+  # Search for a version in the root-directory name of the content's file list:
+  for gz_ending in "tar.gz" "tgz" "tar.bz2" "tbz2" ; do
+    if [ -z "$MYVERSION" ]; then
+      MYVERSION=`tar tf $BASENAME*.${gz_ending} | sed -e 's,/.*,,' | uniq | sed -n "s,$BASENAME.*[-_]\(.*\),\1,p"`
+    else
+      break;
+    fi
+  done
+  # Take version number (and optional revision) from Debian changelog
+  if [ -z "$MYVERSION" ]; then
+    MYVERSION=`head -n 1 *debian.changelog 2>/dev/null | sed -ne 's/.*(\(.*\)).*/\1/p'`
+  fi
+  if [ -z "$MYVERSION" ]; then
+    echo "ERROR: no version is given and can't get detected automatically"
+    exit 1
+  fi
+
+  # remove trailing ".orig", used by dpkg sources
+  MYVERSION=`sed s/.orig$// <<<"$MYVERSION"`
+}
+
+write_files () {
+  if [ -z "$FILES" ]; then
+    FILES="*.spec *.dsc"
+  fi
+  if [ -z "$MYOUTDIR" ]; then
+    echo "ERROR: no output directory is given via --outdir parameter!"
+    exit 1
+  fi
+
+  for i in $FILES; do
+    FILE=`ls -1 $i 2>/dev/null`
+    [ -e "$FILE" ] || continue
+
+    sed "0,/^Version:\(\s*\)[^%]*/s//Version:\1$MYVERSION/" "$FILE" > "$MYOUTDIR/$FILE" || exit 1
+    if [ "${FILE%.spec}" != "$FILE" ]; then
+      # set release back to zero after version upgrade, will be increased by OBS during build
+      # also keep macros in release in case of fedora/mandriva
+      sed -r -i "s,^Release:(\s*)[^%]*,Release:\10," "$MYOUTDIR/$FILE" || exit 1
+   fi
+
+    if [ "${FILE#_service:}" != "$FILE" ]; then
+      # we can remove service files, no need to store them twice
+      rm -f "$FILE"
+    fi
+  done
+
+  if [ -e PKGBUILD ]; then
+    FILE=`ls -1 _service:*[-_]"${MYVERSION}"* | head -n 1`
+    [ -z "$FILE" ] && FILE=`ls -1 *[-_]"${MYVERSION}"* | head -n 1`
+    MD5=`md5sum "$FILE" | head -n 1 | cut -d\  -f 1`
+
+    sed "0,/^pkgver=\(\s*\)[^%]*/s//pkgver=\1$MYVERSION/" PKGBUILD > "$MYOUTDIR/PKGBUILD" || exit 1
+    sed -r -i "s,^pkgrel=.*,pkgrel=0," "$MYOUTDIR/PKGBUILD" || exit 1
+    sed -r -i "s,^md5sums=.*,md5sums=('$MD5')," "$MYOUTDIR/PKGBUILD" || exit 1
+  fi
+}
+
+get_version_from_file
+write_files
+
+exit 0

programming_language: Shell