References: <1175-5f9fbe80-df-25a5a500@198976341> <1174-5fb7e980-5d-37e14c00@74918021> From: Philippe Gerum Subject: Re: Strange switching [threadname] to secondary mode after exception #0 in kernel-space at 0x80272444 In-reply-to: <1174-5fb7e980-5d-37e14c00@74918021> Date: Thu, 03 Dec 2020 14:52:21 +0100 Message-ID: <87a6uv5756.fsf@xenomai.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: Discussions about the Xenomai project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Fran=C3=A7ois?= Legal Cc: "Xenomai@xenomai.org" Fran=C3=A7ois Legal writes: > Le Lundi, Novembre 02, 2020 09:08 CET, Fran=C3=A7ois Legal via Xenomai a =C3=A9crit:=20 >=20=20 >> Le Samedi, Octobre 31, 2020 17:48 CET, Philippe Gerum = a =C3=A9crit:=20 >>=20=20 >> >=20 >> > Fran=C3=A7ois Legal writes: >> >=20 >> > > Le Vendredi, Octobre 16, 2020 18:24 CEST, Philippe Gerum a =C3=A9crit:=20 >> > >=20=20 >> > >>=20 >> > >> Fran=C3=A7ois Legal writes: >> > >>=20 >> > >> > Le Vendredi, Octobre 16, 2020 14:03 CEST, Philippe Gerum a =C3=A9crit:=20 >> > >> >=20=20 >> > >> >>=20 >> > >> >> Fran=C3=A7ois Legal writes: >> > >> >>=20 >> > >> >> > Le Vendredi, Octobre 16, 2020 10:59 CEST, Philippe Gerum a =C3=A9crit:=20 >> > >> >> >=20=20 >> > >> >> >>=20 >> > >> >> >> Fran=C3=A7ois Legal writes: >> > >> >> >>=20 >> > >> >> >> > Le Mercredi, Octobre 14, 2020 16:16 CEST, Greg Gallagher a =C3=A9crit:=20 >> > >> >> >> >=20=20 >> > >> >> >> >> On Wed, Oct 14, 2020 at 5:37 AM Jan Kiszka wrote: >> > >> >> >> >> > >> > >> >> >> >> > On 14.10.20 10:43, Fran=C3=A7ois Legal via Xenomai wrote: >> > >> >> >> >> > > Anybody can help on this ? >> > >> >> >> >> > > >> > >> > >> >> > >> > >> >> >> >> > I'm unfortunately not familiar with the armv7 details of= copy-from-user, >> > >> >> >> >> > not too speak of how spectre contributed to it. Greg, Ph= ilippe, did you >> > >> >> >> >> > come across this already? >> > >> >> >> >> > >> > >> >> >> >> > Jan >> > >> >> >> >> > >> > >> >> >> >> I'll take a look tonight but I haven't hit this in my test= ing. This >> > >> >> >> >> was found on 4.4? Have you tried the 4.19 kernels? >> > >> >> >> >>=20 >> > >> >> >> >> -Greg >> > >> >> >> >=20=20 >> > >> >> >> > So I tried the same case on 4.19.121, with the same result,= and I guess for the same reason. >> > >> >> >> > Could anybody explain why, on ARMv7, we need to copy the me= ssage header at the syscall entry, and not let the xxxmsg routine do it on = its own ? >> > >> >> >> > Also, I could not find how those COBALT_SYSCALL32emu logic = work. >> > >> >> >>=20 >> > >> >> >> There is no way an armv7 system would run the sys32emu code in >> > >> >> >> Cobalt. This code path is specific to architectures which sup= port mixed >> > >> >> >> ABI models. Only Cobalt/x86 supports this so far, issuing x86= _32 >> > >> >> >> syscalls to an x86_64 kernel. You may want to check >> > >> >> >> CONFIG_XENO_ARCH_SYS3264, it is set to "def_bool n" in the Kc= onfig >> > >> >> >> stuff. >> > >> >> >>=20 >> > >> >> > >> > >> >> > Maybe I don't use the right terms here, but what I can see fro= m the code is (in linux tree kernel/xenomai/posix/syscall32.c) >> > >> >> > COBALT_SYSCALL32emu(sendmsg, handover, >> > >> >> > (int fd, struct compat_msghdr __user *umsg, int flags)) >> > >> >> > { >> > >> >> > struct user_msghdr m; >> > >> >> > int ret; >> > >> >> > >> > >> >> > ret =3D sys32_get_msghdr(&m, umsg); >> > >> >> > >> > >> >> > return ret ?: rtdm_fd_sendmsg(fd, &m, flags); >> > >> >> > } >> > >> >> > >> > >> >> > And the problem regarding SPECTRE mitigation is with the "ret = =3D >> > >> >> > sys32_get_msghdr(&m, umsg);" line, as af_packet (in my case, b= ut I >> > >> >> > believe the other handlers should do the same) will also call>= >> >> > copy_from_user on the "msghdr" argument, and the SPECTRE mitigation >> > >> >> > will check that this pointer is in the userland MM area. >> > >> >>=20 >> > >> >> There is indeed a problem with this code passing the kernel memo= ry >> > >> >> address of a bounce buffer to RTDM handlers which would expect _= _user >> > >> >> tagged memory instead. This ends up confusing any low-level >> > >> >> copy_to/from_user routine which includes attack >> > >> >> mitigation. rtnet_get_arg() does call such routine under the hoo= d. This >> > >> >> is something some Xenomai contributor may want to address. >> > >> >>=20 >> > >> >> But, again, this sys32emu code cannot run for armv7 under the cu= rrent >> > >> >> stock implementation. So what we are discussing is purely hypoth= etical >> > >> >> at this stage for this architecture, and should definitely never= happen >> > >> >> by construction if you are running armv7 (which does not make the >> > >> >> original issue go away, that is granted). >> > >> >>=20 >> > >> > >> > >> > I'm not sure I quite understand that point. The code reproduced a= bove is well built in the kernel. Are you saying this code is not called wh= enever userland calls sendmsg on an rt socket ? Am I looking in the wrong d= irection ? In that case, where should I be looking ? I mean, I tracked the = whole thing with a JTAG debugger, and it seemed to me that this was what wa= s really happening, with the SPECTRE code setting the pointer to 0 which wa= s later being caught by arm_copy_from_user. >> > >> > >> > >>=20 >> > >> How could syscall32.c and compat.c be built into the kernel with >> > >> CONFIG_XENO_ARCH_SYS3264 forcibly unset in the Kconfig, which is al= ways >> > >> the case when building for anything else than x86? >> > >>=20 >> > >> Checking kernel/cobalt/posix/Makefile may help in understanding why= it >> > >> is simply not possible. arm_copy_from_user is built in, no question, >> > >> and your analysis regarding SVC context memory being spuriously fed= into >> > >> arm_copy_from_user is likely right. >> > >>=20 >> > >> But the sys32 wrappers are neither for armv7, armv8 nor ppc32. So y= es, >> > >> you are certainly following the wrong path when looking at >> > >> kernel/cobalt/posix/syscall32.c. This 32-to-64bit syscall support i= s NOT >> > >> built into a kernel targeting armv7, at least when it comes to the>= >> vanilla Xenomai code. >> > >>=20 >> > >> You may want to double-check which call site actually invokes >> > >> arm_copy_from_user. >> > >>=20 >> > >> --=20 >> > >> Philippe. >> > >=20=20 >> > > Thanks Philippe for pointing me in the right direction. >> > > So, if I'm correct this time, the problem is about the same, but in = posix/io.c. >> > > In=20 >> > >=20=20 >> > > COBALT_SYSCALL(sendmsg, handover, >> > > (int fd, struct user_msghdr __user *umsg, int flags))> > { >> > > struct user_msghdr m; >> > > int ret; >> > > >> > > ret =3D cobalt_copy_from_user(&m, umsg, sizeof(m)); >> > > >> > > return ret ?: rtdm_fd_sendmsg(fd, &m, flags); >> > > } >> > > >> > > Same thing, the user_msghdr is allocated on the SVC stack, then copi= ed from user, then handed over to the sendmsg handling function pertaining = to that fd, and whenever that handling function call copy_from_user for the= same user_msghdr pointer, it triggers the SPECTRE mitigation protection. >> > > >> > > What is still unclear to me is why this user_msghdr struct is copied= here, and not left to the handling function, as in the sendmmsg syscalls, = the struct mmsghdr is not copied from user in syscall entry. >> > > >> >=20 >> > Unlike sendmsg(), sendmmsg() has to deal with 32bit compat mode, so >> > __rtdm_fd_sendmmsg() receives a couple of helpers along with the user >> > pointer in order to do the right thing, depending on the 32/64 syscall >> > entry. sendmsg() was implemented much earlier too, when SPECTRE >> > mitigation was not there: passing SVC memory to copy_from/to_user> ser= vices was still wrong, but went unnoticed. >> >=20 >> > --=20 >> > Philippe. >>=20=20 >> Alright, so shall I submit a patch removing those copy_from/to_user cal= ls at system call entry in sendmsg and recvmsg ? >> Is this the way to go ? >>=20 >> Thanks >>=20 >> Fran=C3=A7ois >>=20 >>=20 >=20=20 > So for the records, I tried to patch posix/io.c to remove the copy_to/fro= m_user from there to try to avoid the SPECTRE problem, and while that seem = to work perfectly well for the rt_packet endpoint, I come across another pr= oblem in xddp sockets which do expect the struct user_msghdr to be present = in kernel space. > > So I still wonder which is the way to go ? Removing the copy_from/to_user= in posix/io.c and modifying xddp (and possibly other protocols relying on= that) to to add the copy_to/from_user there, or modify only af_packet (and= possibly/probably all sockets type in RTNET) ? > > Thanks > > Fran=C3=A7ois The IPC module implementing XDDP is independent from RTnet, so you would have to fix all offending callers separately, including XDDP. --=20 Philippe.