lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aQIwxjBNonW5Py_I@fedora>
Date: Wed, 29 Oct 2025 20:50:38 +0530
From: Ankit Khushwaha <ankitkhushwaha.linux@...il.com>
To: Andrew Morton <akpm@...ux-foundation.org>
Cc: linux-kselftest@...r.kernel.org, Shuah Khan <shuah@...nel.org>,
	Bala-Vignesh-Reddy <reddybalavignesh9979@...il.com>,
	Wei Yang <richard.weiyang@...il.com>, linux-kernel@...r.kernel.org,
	linux-kernel-mentees@...ts.linuxfoundation.org,
	Steven Rostedt <rostedt@...dmis.org>
Subject: Re: [PATCH] selftests/user_events: Avoid taking address of packed
 member in perf_test

On Tue, Oct 28, 2025 at 01:26:05PM -0700, Andrew Morton wrote:
> On Tue, 28 Oct 2025 22:28:10 +0530 Ankit Khushwaha <ankitkhushwaha.linux@...il.com> wrote:
> 
> > > > @@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
> > > >  	ASSERT_EQ(1 << reg.enable_bit, self->check);
> > > >  
> > > >  	/* Ensure write shows up at correct offset */
> > > > -	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
> > > > +	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
> > > > +	ASSERT_NE(-1, write(self->data_fd, &write_index,
> > > >  	                    sizeof(reg.write_index)));
> > > 
> > > Simply casting &write_index to void* would fix this?
> > 
> > yes, this hides the type mismatch from the compiler. But i think
> > casting to void * will not fix the alignment mismatch for packed struct.
> > It works on x86, but might break on other platform.
> 
> It's the second argument to write(2)!  write(2) expects a const char *,
> but void* will work.

Hi Andrew,
Indeed 
`ASSERT_NE(-1, write(self->data_fd, (void *)&reg.write_index, 
		     sizeof(reg.write_index)));`

would work. However since `reg` is packed struct, directly taking the 
address of its member  `&reg.write_index` may lead to unaligned access 
on some architectures. as indicated by the compiler warning

	perf_test.c:239:38: warning: taking address of packed member
	'write_index' of class or structure 'user_reg' may result in 
	an unaligned pointer value [-Waddress-of-packed-member]
	239 |         ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
      	    |						  ^~~~~~~~~~~~~~~

Using `memcpy` avoids this by performing a byte-wise copy, which is safe 
to use for packed structures.

Thanks
-- Ankit

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ