From 0ea5bdf8fc8abff6dfc279176311f6813685622d Mon Sep 17 00:00:00 2001 From: schary842 Date: Sun, 22 Nov 2026 04:59:19 +0000 Subject: [PATCH] RDKEMW-28109: Fix high severity security findings in sysint scripts --- lib/rdk/getAccountId.sh | 6 +- lib/rdk/getDeviceDetails.sh | 139 ++++++++++++++++++++++++------- lib/rdk/getDeviceId.sh | 4 +- lib/rdk/getPartnerId.sh | 9 +- lib/rdk/timesyncd-conf-update.sh | 9 +- lib/rdk/xconfImageCheck.sh | 3 +- 6 files changed, 130 insertions(+), 40 deletions(-) diff --git a/lib/rdk/getAccountId.sh b/lib/rdk/getAccountId.sh index 1a0737e5f..a457c92f2 100644 --- a/lib/rdk/getAccountId.sh +++ b/lib/rdk/getAccountId.sh @@ -37,7 +37,11 @@ getAccountId() echo "$$ : `cat /proc/$$/cmdline`" >> $ACCOUNT_ID_LOG if [ "$accountId" != "" ] ; then - if [ "$accountIdLen" -lt "$KEY_LEN" ] && [ `expr "$accountId" : ".*[a-zA-Z0-9_-].*"` -gt 0 ] && [ `expr "$accountId" : ".*[!@#\$%^\&*()_+].*"` -eq 0 ]; then + case "$accountId" in + *[!A-Za-z0-9_-]*) accountIdValid=0 ;; + *) accountIdValid=1 ;; + esac + if [ "$accountIdLen" -lt "$KEY_LEN" ] && [ "$accountIdValid" -eq 1 ]; then echo "`/bin/timestamp` : accountId is valid and value retrieved from tr181 param." >> $ACCOUNT_ID_LOG echo "$accountId" else diff --git a/lib/rdk/getDeviceDetails.sh b/lib/rdk/getDeviceDetails.sh index ef9ff6225..30cfb0de3 100755 --- a/lib/rdk/getDeviceDetails.sh +++ b/lib/rdk/getDeviceDetails.sh @@ -217,7 +217,8 @@ getManufacturer(){ output=$(mfr_util --Manufacturer 2>&1) if [ -n "$output" ] && ! echo "$output" | grep -iq "failed"; then output=$(echo $output | sed 's/ /_/g') - echo "$output" | tee /tmp/.manufacturer + echo "$output" | safeWrite /tmp/.manufacturer + echo "$output" else echo "UNKNOWN" fi @@ -226,7 +227,7 @@ getManufacturer(){ getBrandName(){ output=$(mfr_util --Manufacturer 2>&1) if [ -n "$output" ] && ! echo "$output" | grep -iq "failed"; then - echo "$output" > /tmp/.brandname + echo "$output" | safeWrite /tmp/.brandname fi } @@ -307,6 +308,22 @@ getRF4CEMac() } +# Write stdin to a /tmp file atomically: build a fresh mktemp file (O_EXCL, +# root-owned) and rename it over the target. rename() replaces a symlink +# instead of following it, and readers never see a missing/partial file. +safeWrite() +{ + file="$1" + [ -d "$file" ] && return 1 + sw_tmp=$(mktemp "${file}.XXXXXX") || return 1 + if cat > "$sw_tmp" && chmod 644 "$sw_tmp" && mv -f "$sw_tmp" "$file"; then + return 0 + fi + rm -f "$sw_tmp" + logMsg "Error: unable to securely write $file" + return 1 +} + executeServiceRequest() { logMsg "exec service = $1" @@ -396,50 +413,50 @@ executeServiceRequest() ;; "downloadIP" | "ecm_ip") getEcmIp - echo "$ecmIp" > /tmp/.downloadIP - echo "$ecmIp" > /tmp/.ecm_ip + echo "$ecmIp" | safeWrite /tmp/.downloadIP + echo "$ecmIp" | safeWrite /tmp/.ecm_ip ;; "boxIP" | "estb_ip") getBoxIPAddress - echo "$IPAddress" > /tmp/.boxIP - echo "$IPAddress" > /tmp/.estb_ip + echo "$IPAddress" | safeWrite /tmp/.boxIP + echo "$IPAddress" | safeWrite /tmp/.estb_ip ;; "macAddress" | "estb_mac") MacAddress=`getEstbMacAddress` - echo "$MacAddress" > /tmp/.macAddress - echo "$MacAddress" > /tmp/.estb_mac + echo "$MacAddress" | safeWrite /tmp/.macAddress + echo "$MacAddress" | safeWrite /tmp/.estb_mac ;; "ecm_mac") getEcmMac - echo "$EcmMac" > /tmp/.ecm_mac - echo "$EcmMac" | tr ':' ' ' > /tmp/ecm_mac.txt + echo "$EcmMac" | safeWrite /tmp/.ecm_mac + echo "$EcmMac" | tr ':' ' ' | safeWrite /tmp/ecm_mac.txt ;; "eth_mac") getEthernetMacAddress - echo "$EtherMac" > /tmp/.eth_mac + echo "$EtherMac" | safeWrite /tmp/.eth_mac ;; "moca_mac") getMocaMac - echo "$MocaMac" > /tmp/.moca_mac + echo "$MocaMac" | safeWrite /tmp/.moca_mac ;; "wifi_mac") [ -f /proc/device-tree/wifi-mac-addr ] && WiFiMac=$(cat /proc/device-tree/wifi-mac-addr) || WiFiMac= if [ "$WiFiMac" == "" ]; then getWiFiMac fi - echo "$WiFiMac" > /tmp/.wifi_mac + echo "$WiFiMac" | safeWrite /tmp/.wifi_mac ;; "moca_ip") getMocaIp - echo "$MocaIp" > /tmp/.moca_ip + echo "$MocaIp" | safeWrite /tmp/.moca_ip ;; "model") getModel - echo "$model" > /tmp/.model + echo "$model" | safeWrite /tmp/.model ;; "model_number") modelNum=`getModelNum` - echo "$modelNum" > /tmp/.model_number + echo "$modelNum" | safeWrite /tmp/.model_number ;; "manufacturer") getManufacturer @@ -449,39 +466,39 @@ executeServiceRequest() ;; "device_type") deviceType=`getDeviceType` - echo "$deviceType" > /tmp/.device_type + echo "$deviceType" | safeWrite /tmp/.device_type ;; "friendly_id") friendlyId=`getFriendlyId` - echo "$friendlyId" > /tmp/.friendly_id + echo "$friendlyId" | safeWrite /tmp/.friendly_id ;; "imageVersion") getFWVersion - echo "$Version" > /tmp/.imageVersion + echo "$Version" | safeWrite /tmp/.imageVersion ;; "cableCardVersion") getCableCardVersion - echo "$CableCardVersion" > /tmp/.cableCardVersion + echo "$CableCardVersion" | safeWrite /tmp/.cableCardVersion ;; "build_type") buildType=`getBuildType` - echo "$buildType" > /tmp/.build_type + echo "$buildType" | safeWrite /tmp/.build_type ;; "DACInitTimestamp") DACInitTimestamp=`getDACInitTimestamp` - echo "$DACInitTimestamp" > /tmp/.DACInitTimestamp + echo "$DACInitTimestamp" | safeWrite /tmp/.DACInitTimestamp ;; "serial_number") getDeviceSerialNumber - echo "$serialNumber" > /tmp/.serial_number + echo "$serialNumber" | safeWrite /tmp/.serial_number ;; "bluetooth_mac") BluetoothMac=`getBluetoothMac` - echo "$BluetoothMac" > /tmp/.bluetooth_mac + echo "$BluetoothMac" | safeWrite /tmp/.bluetooth_mac ;; "rf4ce_mac") RF4CEMac=`getRF4CEMac` - echo "$RF4CEMac" > /tmp/.rf4ce_mac + echo "$RF4CEMac" | safeWrite /tmp/.rf4ce_mac ;; "top_labels") echo @@ -498,7 +515,19 @@ executeServiceRequest() esac if [ -n "$1" ] && [ "$1" != "all" ]; then - [ ! -f "$deviceDetailsCache" ] && executeServiceRequestOutput || sed -i 's/'"$1"'=.*/'"$1"'='"$(cat /tmp/."$1")"'/' "$deviceDetailsCache" + if [ ! -f "$deviceDetailsCache" ]; then + executeServiceRequestOutput + elif echo "$1" | grep -Eq '^[A-Za-z0-9:._-]+$' && [ -f "/tmp/.$1" ] && [ ! -L "/tmp/.$1" ]; then + tmpfile=$(mktemp) + value=$(head -n 1 "/tmp/.$1") + awk -v key="$1" -v val="$value" -F= ' + BEGIN{OFS="="} + $1==key {$2=val} + {print} + ' "$deviceDetailsCache" > "$tmpfile" && + chmod 644 "$tmpfile" && + mv "$tmpfile" "$deviceDetailsCache" + fi unlock fi } @@ -526,14 +555,32 @@ executeServiceRequestOutput() updateMissingParameters() { - data=$(sed -n '/=$/p' /tmp/.deviceDetails.cache | tr "=" " ") + # /tmp is world-writable: ignore a cache that is a symlink or not owned by us + if [ -L "$deviceDetailsCache" ] || [ ! -f "$deviceDetailsCache" ] || [ ! -O "$deviceDetailsCache" ]; then + return 0 + fi + data=$(sed -n '/=$/p' "$deviceDetailsCache" | tr "=" " ") if [ "$data" != "" ]; then echo "$data" | while read -r param do - if [ "$param" != "" ]; then - file=/tmp/.$param - [ -f $file ] && [ "$(cat $file)" != "" ] && sed -i 's/'$param=.*'/'$param="$(cat $file)"'/' "$deviceDetailsCache" - [ ! -f $file ] || [ "$(cat $file)" == "" ] && executeServiceRequest $param + # only known parameter names are used to build paths and commands + case "$param" in + estb_mac|ecm_mac|moca_mac|wifi_mac|eth_mac|model|model_number|device_type|friendly_id|build_type|imageVersion|ecm_ip|estb_ip|moca_ip|boxIP|cableCardVersion|DACInitTimestamp|serial_number|bluetooth_mac|rf4ce_mac) ;; + *) continue ;; + esac + file="/tmp/.$param" + if [ -f "$file" ] && [ ! -L "$file" ] && [ "$(head -n 1 "$file")" != "" ]; then + tmpfile=$(mktemp) + value=$(head -n 1 "$file") + awk -v key="$param" -v val="$value" -F= ' + BEGIN{OFS="="} + $1==key {$2=val} + {print} + ' "$deviceDetailsCache" > "$tmpfile" && + chmod 644 "$tmpfile" && + mv "$tmpfile" "$deviceDetailsCache" + else + executeServiceRequest "$param" fi done fi @@ -574,6 +621,25 @@ unlock() # execute service request with arguments if [ "$command" != "" ]; then + # /tmp is world-writable: discard any cache or /tmp/. file that is a + # symlink or not owned by the running user so it is regenerated, never trusted + if [ -e "$deviceDetailsCache" ] || [ -L "$deviceDetailsCache" ]; then + if [ -L "$deviceDetailsCache" ] || [ ! -O "$deviceDetailsCache" ]; then + rm -f "$deviceDetailsCache" + fi + fi + if [ -n "$parameter" ] && [ "$parameter" != "all" ]; then + if ! echo "$parameter" | grep -Eq '^[A-Za-z0-9:._-]+$'; then + logMsg "Error: invalid parameter" + exit 1 + fi + if [ -e "/tmp/.$parameter" ] || [ -L "/tmp/.$parameter" ]; then + if [ -L "/tmp/.$parameter" ] || [ ! -f "/tmp/.$parameter" ] || [ ! -O "/tmp/.$parameter" ]; then + rm -f "/tmp/.$parameter" + fi + fi + fi + if [ ! -s "$deviceDetailsCache" ]; then executeServiceRequestOutput fi @@ -594,7 +660,16 @@ if [ "$command" != "" ]; then file=/tmp/."$parameter" [ ! -f "$file" ] || [ "$(cat $file)" == "" ] && executeServiceRequest "$parameter" [ -f "$deviceDetailsCache" ] && value=$(cat $deviceDetailsCache | grep $parameter | cut -d "=" -f2) - [ -f "$deviceDetailsCache" ] && [ "$value" == "" ] && sed -i 's/'$parameter=.*'/'$parameter="$(cat $file)"'/' $deviceDetailsCache + if [ -f "$deviceDetailsCache" ] && [ "$value" == "" ] && echo "$parameter" | grep -Eq '^[A-Za-z0-9:._-]+$'; then + tmpfile=$(mktemp) + fileValue=$(cat "$file") + awk -v key="$parameter" -v val="$fileValue" -F= ' + BEGIN{OFS="="} + $1==key {$2=val} + {print} + ' "$deviceDetailsCache" > "$tmpfile" && + mv "$tmpfile" "$deviceDetailsCache" + fi [ -f "$file" ] && cat $file else diff --git a/lib/rdk/getDeviceId.sh b/lib/rdk/getDeviceId.sh index f95e1f3ab..340da5d4a 100644 --- a/lib/rdk/getDeviceId.sh +++ b/lib/rdk/getDeviceId.sh @@ -21,7 +21,6 @@ . /etc/include.properties . /etc/device.properties BSFILE='/opt/secure/RFC/bootstrap.ini' -FILENAME='/tmp/.bootstrap.ini' deviceIdFile="/opt/www/authService/deviceid.dat" partnerIdFile="/opt/www/authService/partnerId3.dat" @@ -44,8 +43,7 @@ getBootstrapPartnerId() { if [ -f "$BSFILE" ]; then - cp $BSFILE $FILENAME - value2=`grep -i 'X_RDKCENTRAL-COM_Syndication.PartnerId' $FILENAME | awk 'BEGIN{FS="=";};{print $2}' ` + value2=$(grep -i 'X_RDKCENTRAL-COM_Syndication.PartnerId' "$BSFILE" | cut -d= -f2-) if [ "$value2" ]; then echo "$value2"; return ; diff --git a/lib/rdk/getPartnerId.sh b/lib/rdk/getPartnerId.sh index da6548c35..0fd19a117 100644 --- a/lib/rdk/getPartnerId.sh +++ b/lib/rdk/getPartnerId.sh @@ -55,7 +55,11 @@ getReceiverId() getPartnerId() { if [ "$WHOAMI_SUPPORT" == "true" ]; then - partnerName="$(tr181 -g Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Bootstrap.PartnerName 2>&1)" + partnerName="$(tr181 -g Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Bootstrap.PartnerName 2>/dev/null)" + # Allowlist: value is later embedded in a shell command line + case "$partnerName" in + *[!A-Za-z0-9._-]*) partnerName="" ;; + esac echo "$partnerName" return; fi @@ -64,6 +68,9 @@ getPartnerId() sh /lib/rdk/getDeviceId.sh > /tmp/partnerid.txt output=`awk -F',' /tmp/.signedRequest + GetServiceUrl $request_type "$JSONSTR" > /tmp/.signedRequest if [ -s /tmp/.signedRequest ] then echo "GetServiceUrl success"