Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rdk/getAccountId.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
139 changes: 107 additions & 32 deletions lib/rdk/getDeviceDetails.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -574,6 +621,25 @@ unlock()
# execute service request with arguments
if [ "$command" != "" ]; then

# /tmp is world-writable: discard any cache or /tmp/.<param> 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
Comment on lines +626 to +627
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
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/rdk/getDeviceId.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 ;
Expand Down
9 changes: 8 additions & 1 deletion lib/rdk/getPartnerId.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,6 +68,9 @@ getPartnerId()
sh /lib/rdk/getDeviceId.sh > /tmp/partnerid.txt
output=`awk -F',' </tmp/partnerid.txt '{ for (i=1; i<=NF; i++) print $i}'| grep partnerId | cut -d ":" -f2 | tr -d " " |sed -e 's/[{,},/"]//g'`
partnerName=`echo "$output" | tr '[A-Z]' '[a-z]'`
case "$partnerName" in
*[!A-Za-z0-9._-]*) partnerName="" ;;
esac
Comment on lines +71 to +73

if [ "$partnerName" != "" ]; then
echo "$partnerName"
Expand Down
9 changes: 8 additions & 1 deletion lib/rdk/timesyncd-conf-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ if [ -f /etc/systemd/timesyncd.conf ];then
defaultHostName2=''
fi
for host in $partnerHostnames; do
if grep -q "$host" /etc/systemd/timesyncd.conf; then
# Allowlist: hostnames are interpolated into a sed program below
case "$host" in
*[!A-Za-z0-9.-]*)
ntpLog "Ignoring invalid NTP hostname"
continue
;;
esac
if grep -qF -- "$host" /etc/systemd/timesyncd.conf; then
updateHostnames+=" "
else
updateHostnames+="$host "
Expand Down
3 changes: 1 addition & 2 deletions lib/rdk/xconfImageCheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ sendXCONFCodebigRequest () {
echo ADDITIONAL_FW_VER_INFO: $pdriFwVerInfo$remoteInfo
t2ValNotify "RCU_FWver_split" "$pdriFwVerInfo$remoteInfo"
fi
SIGN_CMD="GetServiceUrl $request_type \"$JSONSTR\""
eval $SIGN_CMD > /tmp/.signedRequest
GetServiceUrl $request_type "$JSONSTR" > /tmp/.signedRequest
if [ -s /tmp/.signedRequest ]
then
echo "GetServiceUrl success"
Expand Down