From 81543253cfe9fb4649b4b00058325c092a6b5259 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sat, 17 Oct 2020 23:28:26 +0200 Subject: [PATCH] Fix misaligned access inside taskstats structure Reported by UB sanitizer (alongside several other messages): linux/LinuxProcessList.c:782:25: runtime error: member access within misaligned address 0x614000000264 for type 'struct taskstats', which requires 8 byte alignment 0x614000000264: note: pointer points here 64 01 03 00 0a 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 4b c8 2e 00 00 00 00 00 3e 45 3c fd ^ The issue doesn't cause trouble on x86, but any architecture with stricter memory alignment requirements may inadvertedly break. --- linux/LinuxProcessList.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 57fab842..bbb064d1 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -766,7 +766,7 @@ static int handleNetlinkMsg(struct nl_msg *nlmsg, void *linuxProcess) { struct nlmsghdr *nlhdr; struct nlattr *nlattrs[TASKSTATS_TYPE_MAX + 1]; struct nlattr *nlattr; - struct taskstats *stats; + struct taskstats stats; int rem; unsigned long long int timeDelta; LinuxProcess* lp = (LinuxProcess*) linuxProcess; @@ -778,20 +778,21 @@ static int handleNetlinkMsg(struct nl_msg *nlmsg, void *linuxProcess) { } if ((nlattr = nlattrs[TASKSTATS_TYPE_AGGR_PID]) || (nlattr = nlattrs[TASKSTATS_TYPE_NULL])) { - stats = nla_data(nla_next(nla_data(nlattr), &rem)); - assert(lp->super.pid == (pid_t)stats->ac_pid); - timeDelta = (stats->ac_etime*1000 - lp->delay_read_time); + memcpy(&stats, nla_data(nla_next(nla_data(nlattr), &rem)), sizeof(stats)); + assert(lp->super.pid == (pid_t)stats.ac_pid); + + timeDelta = (stats.ac_etime*1000 - lp->delay_read_time); #define BOUNDS(x) isnan(x) ? 0.0 : (x > 100) ? 100.0 : x; #define DELTAPERC(x,y) BOUNDS((float) (x - y) / timeDelta * 100); - lp->cpu_delay_percent = DELTAPERC(stats->cpu_delay_total, lp->cpu_delay_total); - lp->blkio_delay_percent = DELTAPERC(stats->blkio_delay_total, lp->blkio_delay_total); - lp->swapin_delay_percent = DELTAPERC(stats->swapin_delay_total, lp->swapin_delay_total); + lp->cpu_delay_percent = DELTAPERC(stats.cpu_delay_total, lp->cpu_delay_total); + lp->blkio_delay_percent = DELTAPERC(stats.blkio_delay_total, lp->blkio_delay_total); + lp->swapin_delay_percent = DELTAPERC(stats.swapin_delay_total, lp->swapin_delay_total); #undef DELTAPERC #undef BOUNDS - lp->swapin_delay_total = stats->swapin_delay_total; - lp->blkio_delay_total = stats->blkio_delay_total; - lp->cpu_delay_total = stats->cpu_delay_total; - lp->delay_read_time = stats->ac_etime*1000; + lp->swapin_delay_total = stats.swapin_delay_total; + lp->blkio_delay_total = stats.blkio_delay_total; + lp->cpu_delay_total = stats.cpu_delay_total; + lp->delay_read_time = stats.ac_etime*1000; } return NL_OK; }