eclair: use snprintf instead of sprintf to prevent buffer-overrun

Change-Id: I2a27cffe303201e1738f115258f6e02058dbc63d
Refs: #1356
Fujitsu: POSTK_DEBUG_ARCH_DEP_38
This commit is contained in:
Ken Sato
2019-09-09 15:09:10 +09:00
committed by Masamichi Takagi
parent beac6c3e80
commit a4b83dc6d4
5 changed files with 267 additions and 8 deletions

View File

@ -678,7 +678,6 @@ static ssize_t print_hex(char *buf, size_t buf_size, char *str) {
q = buf;
for (p = str; *p != '\0'; ++p) {
#ifdef POSTK_DEBUG_ARCH_DEP_38
int ret;
ret = snprintf(q, buf_size, "%02x", *p);
@ -687,9 +686,6 @@ static ssize_t print_hex(char *buf, size_t buf_size, char *str) {
}
q += ret;
buf_size -= ret;
#else /* POSTK_DEBUG_ARCH_DEP_38 */
q += sprintf(q, "%02x", *p);
#endif /* POSTK_DEBUG_ARCH_DEP_38 */
}
*q = '\0';
@ -704,7 +700,6 @@ ssize_t print_bin(char *buf, size_t buf_size, void *data, size_t size) {
p = data;
q = buf;
for (i = 0; i < size; ++i) {
#ifdef POSTK_DEBUG_ARCH_DEP_38
int ret;
ret = snprintf(q, buf_size, "%02x", *p);
@ -713,9 +708,6 @@ ssize_t print_bin(char *buf, size_t buf_size, void *data, size_t size) {
}
q += ret;
buf_size -= ret;
#else /* POSTK_DEBUG_ARCH_DEP_38 */
q += sprintf(q, "%02x", *p);
#endif /* POSTK_DEBUG_ARCH_DEP_38 */
++p;
}
*q = '\0';

135
test/issues/1356/C1356.sh Executable file
View File

@ -0,0 +1,135 @@
#!/bin/sh
USELTP=0
USEOSTEST=0
BOOTPARAM="-c 1-7-m 10G@0,10G@1 -O"
. ../../common.sh
issue=1356
tid=01
arch=`uname -p`
if [ "${arch}" == "x86_64" ]; then
ARCHDIR="smp-x86"
elif [ "${arch}" == "aarch64" ]; then
ARCHDIR="smp-arm64"
else
echo "Not supported architecture."
exit 1
fi
ECLAIR=${BIN}/eclair
MCKDUMP=/tmp/issue${issue}_mckdump
MCKIMG=${BIN}/../${ARCHDIR}/kernel/mckernel.img
LOG=./C${issue}_eclair.txt
# dump mckdump
sleep 1
echo "** Dump Mckernel-dump"
sudo ${IHKOSCTL} 0 dump -d 24 ${MCKDUMP}
sleep 1
echo "** DONE ${MCKDUMP}"
echo ""
echo "***** Result of eclair ************************"
expect -c "
set timeout 20
spawn ${ECLAIR} -d ${MCKDUMP} -k ${MCKIMG} -l
expect \"(eclair)\"
send \"set pagination 0\n\"
expect \"(eclair)\"
send \"info threads\n\"
expect \"(eclair)\"
send \"thread 3\n\"
expect \"(eclair)\"
send \"info register\n\"
expect \"(eclair)\"
send \"bt\n\"
expect \"(eclair)\"
send \"quit\n\
" | tee ./${LOG}
tname=`printf "C${issue}T%02d" ${tid}`
echo "*** ${tname} start *******************************"
echo "** Check reading symbols"
grep -o "Reading symbols.*mckernel.img...done." ${LOG}
if [ $? -eq 0 ]; then
echo "*** ${tname} PASSED ******************************"
else
echo "*** ${tname} FAILED ******************************"
fi
let tid++
echo ""
tname=`printf "C${issue}T%02d" ${tid}`
echo "*** ${tname} start *******************************"
echo "** Check detected number of cpus"
expect_num=`${IHKOSCTL} 0 kmsg | grep "# of cpus" | cut -d " " -f 7`
cat ${LOG} | awk '/info threads/,/thread 3/' > ${tname}.txt
thread_num=`cat ${tname}.txt | grep "New Thread" | wc -l`
cpu_num=$(( $thread_num + 1 ))
echo "** CPU_NUM: ${cpu_num} (expected ${expect_num})"
if [ ${cpu_num} -eq ${expect_num} ]; then
echo "*** ${tname} PASSED ******************************"
else
echo "*** ${tname} FAILED ******************************"
fi
let tid++
echo ""
tname=`printf "C${issue}T%02d" ${tid}`
echo "*** ${tname} start *******************************"
echo "** Check switch thread"
cat ${LOG} | awk '/thread 3/,/info register/' > ${tname}.txt
grep "Switching to thread" ./${tname}.txt
if [ $? -eq 0 ]; then
echo "*** ${tname} PASSED ******************************"
else
echo "*** ${tname} FAILED ******************************"
fi
let tid++
echo ""
tname=`printf "C${issue}T%02d" ${tid}`
echo "*** ${tname} start *******************************"
echo "** Check info register"
cat ${LOG} | awk '/info register/,/bt/' > ${tname}.txt
grep "rip.*cpu_safe_halt.*" ./${tname}.txt
if [ $? -eq 0 ]; then
echo "*** ${tname} PASSED ******************************"
else
echo "*** ${tname} FAILED ******************************"
fi
let tid++
echo ""
tname=`printf "C${issue}T%02d" ${tid}`
echo "*** ${tname} start *******************************"
echo "** Check back trace"
cat ${LOG} | awk '/bt/,/EOF/' > ${tname}.txt
grep "cpu_safe_halt" ./${tname}.txt
if [ $? -eq 0 ]; then
echo "*** ${tname} PASSED ******************************"
else
echo "*** ${tname} FAILED ******************************"
fi
let tid++
echo ""

11
test/issues/1356/Makefile Normal file
View File

@ -0,0 +1,11 @@
CFLAGS=-g
LDFLAGS=
TARGET=
all: $(TARGET)
test: all
./C1356.sh
clean:
rm -f $(TARGET) *.o *.txt

29
test/issues/1356/README Normal file
View File

@ -0,0 +1,29 @@
【Issue#1356 動作確認】
□ テスト内容
1. eclairの基本操作が正常に動作することを確認
McKernelの起動後、ihkosctl dump によってコアダンプを作成し、
以下の操作について動作を確認する
C1356T01:
eclairでダンプファイルを開き、mckernel.img のsymobolsを
読み取れることを確認
C1356T02:
info threadコマンドを実行し、McKernelが利用しているCPU数と
同じ数のthreadが認識できていることを確認
C1356T03:
threadコマンドを実行し、threadの変更が可能なことを確認
C1356T04:
info registerコマンドを実行し、レジスタの情報を表示できることを確認
C1356T05:
btコマンドを実行し、バックトレースを表示できることを確認
□ 実行手順
$ make test
McKernelのインストール先や、OSTEST, LTPの配置場所は、
$HOME/.mck_test_config を参照している
.mck_test_config は、McKernelをビルドした際に生成されるmck_test_config.sample ファイルを
$HOMEにコピーし、適宜編集する
□ 実行結果
x86_64_result.log, aarch64_result.log 参照。
すべての項目をPASSしていることを確認。

View File

@ -0,0 +1,92 @@
** Dump Mckernel-dump
** DONE /tmp/issue1356_mckdump
***** Result of eclair ************************
spawn /home/satoken/ihk+mckernel/bin/eclair -d /tmp/issue1356_mckdump -k /home/satoken/ihk+mckernel/bin/../smp-x86/kernel/mckernel.img -l
eclair 0.20160314
setup_threads: num_processors: 7
Reading symbols from /home/satoken/ihk+mckernel/smp-x86/kernel/mckernel.img...done.
Remote debugging using :33646
cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
1145 }
(eclair) set pagination 0
(eclair) info threads
[New Thread 2000000001]
[New Thread 2000000002]
[New Thread 2000000003]
[New Thread 2000000004]
[New Thread 2000000005]
[New Thread 2000000006]
Id Target Id Frame
7 Thread 2000000006 (PID 1, idle waiting on cpu 6) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
6 Thread 2000000005 (PID 1, idle waiting on cpu 5) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
5 Thread 2000000004 (PID 1, idle waiting on cpu 4) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
4 Thread 2000000003 (PID 1, idle waiting on cpu 3) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
3 Thread 2000000002 (PID 1, idle waiting on cpu 2) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
2 Thread 2000000001 (PID 1, idle waiting on cpu 1) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
* 1 Thread 2000000000 (PID 1, idle waiting on cpu 0) cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
(eclair) thread 3
[Switching to thread 3 (Thread 2000000002)]
#0 cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
1145 }
(eclair) info register
rax 0xfffffffffe856a60 -24810912
rbx 0xffff800100ada438 -140733182008264
rcx 0xfffffffffe8c8318 -24345832
rdx 0xffff880100b1a000 -131937088724992
rsi 0x40004 262148
rdi 0x2 2
rbp 0xffff880100b09f18 0xffff880100b09f18
rsp 0xffff880100b09f18 0xffff880100b09f18
r8 0x0 0
r9 0x0 0
r10 0xffff880100b1c640 -131937088715200
r11 0x0 0
r12 0x46 70
r13 0x0 0
r14 0x0 0
r15 0xfffffffffe856aa0 -24810848
rip 0xfffffffffe856a66 0xfffffffffe856a66 <cpu_safe_halt+6>
eflags 0x286 [ PF SF IF ]
cs 0x20 32
ss 0x28 40
ds 0x28 40
es 0x9000 36864
fs 0x0 0
gs 0x0 0
(eclair) bt
#0 cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
#1 0xfffffffffe82f83a in idle () at /home/satoken/src/mckernel/kernel/process.c:2983
#2 0x0000000000000003 in ?? ()
#3 0x0000000000000008 in ?? ()
#4 0xffff880100b0e000 in ?? ()
#5 0x0000000000000000 in ?? ()
(eclair) missing "
while executing
"send "quit\n
"
*** C1356T01 start *******************************
** Check reading symbols
Reading symbols from /home/satoken/ihk+mckernel/smp-x86/kernel/mckernel.img...done.
*** C1356T01 PASSED ******************************
*** C1356T02 start *******************************
** Check detected number of cpus
** CPU_NUM: 7 (expected 7)
*** C1356T02 PASSED ******************************
*** C1356T03 start *******************************
** Check switch thread
[Switching to thread 3 (Thread 2000000002)]
*** C1356T03 PASSED ******************************
*** C1356T04 start *******************************
** Check info register
rip 0xfffffffffe856a66 0xfffffffffe856a66 <cpu_safe_halt+6>
*** C1356T04 PASSED ******************************
*** C1356T05 start *******************************
** Check back trace
#0 cpu_safe_halt () at /home/satoken/src/mckernel/arch/x86_64/kernel/cpu.c:1145
*** C1356T05 PASSED ******************************