mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Drew B." <subs@mu-ori.me>
To: linux-kernel@vger.kernel.org
Subject: Misbehavior with setsockopt timeval structure with -fpack-struct enabled
Date: Wed, 19 Jul 2023 11:03:19 +0000	[thread overview]
Message-ID: <559f4003c263a7aaa873cbc80947cc57@mu-ori.me> (raw)

Hi everyone!
I've got a very strange behavior on Linux and OS X build of the same 
source code. To be specific, when I try to set socket timeout option:

...
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 1;
ret = setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &timeout, 
sizeof(timeout));
...

with -fpack-struct enabled, on Linux machine the size of timeval struct 
is 16 bytes (as well as unpacked), while on OS X it's 12 for packed and 
16 for unpacked. In which case I get an error while trying to apply the 
setting to the socket. I dug a little bit deeper and the following piece 
of code in net/core/sock.c:

...
int sock_copy_user_timeval(struct __kernel_sock_timeval *tv,
			   sockptr_t optval, int optlen, bool old_timeval)
{
	if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
		struct old_timeval32 tv32;

		if (optlen < sizeof(tv32))
			return -EINVAL;

		if (copy_from_sockptr(&tv32, optval, sizeof(tv32)))
			return -EFAULT;
		tv->tv_sec = tv32.tv_sec;
		tv->tv_usec = tv32.tv_usec;
	} else if (old_timeval) {
		struct __kernel_old_timeval old_tv;

		if (optlen < sizeof(old_tv))
			return -EINVAL;
		if (copy_from_sockptr(&old_tv, optval, sizeof(old_tv)))
			return -EFAULT;
		tv->tv_sec = old_tv.tv_sec;
		tv->tv_usec = old_tv.tv_usec;
	} else {
		if (optlen < sizeof(*tv))
			return -EINVAL;
		if (copy_from_sockptr(tv, optval, sizeof(*tv)))
			return -EFAULT;
	}

	return 0;
}
EXPORT_SYMBOL(sock_copy_user_timeval);
...

So, to be specific (same or similar logics goes through the function in 
respective places):
if (optlen < sizeof(tv32))
	return -EINVAL;
Which means, that it doesn't consider whether the structure is packed or 
not, it always compares against unpacked (?) structure == 16 bytes (for 
now).

Is it expected?

Kind regards,
Drew.

             reply	other threads:[~2023-07-19 11:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-19 11:03 Drew B. [this message]
     [not found] ` <8049a5598fe54002851b2224ada58209@AcuMS.aculab.com>
2023-07-19 14:14   ` Drew B.
     [not found]     ` <4ae99731d4b54c1d98f6b77f6e67d295@AcuMS.aculab.com>
2023-07-19 14:42       ` Drew B.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=559f4003c263a7aaa873cbc80947cc57@mu-ori.me \
    --to=subs@mu-ori.me \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®