Bug 19622 - Support aliasing with struct sockaddr
Summary: Support aliasing with struct sockaddr
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: network (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: 2.40
Assignee: Florian Weimer
URL:
Keywords:
: 30293 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-02-12 12:34 UTC by Florian Weimer
Modified: 2024-06-13 07:40 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Weimer 2016-02-12 12:34:34 UTC
It is quite common to pass pointers to struct sockaddr_in and similar socket structures to as struct sockaddr pointers, or to peak at a socket address through a struct sockaddr pointer to look at the sa_family member to determine the concrete socket address type.

This breaks strict aliasing.  Marek Polacek suggests to add the may_alias attribute to struct sockaddr.
Comment 1 Florian Weimer 2016-02-15 10:52:55 UTC
This does not work as expected because it breaks forward declarations:

struct sockaddr;
struct sockaddr *f();

struct __attribute__((may_alias)) sockaddr {};
struct sockaddr *f()
{
  return nullptr;
}

results in:

t.cc: In function ‘sockaddr* f()’:
t.cc:5:18: error: ambiguating new declaration of ‘sockaddr* f()’
 struct sockaddr *f()
                  ^

t.cc:2:18: note: old declaration ‘sockaddr* f()’
 struct sockaddr *f();
                  ^
Comment 2 Florian Weimer 2016-02-15 16:46:08 UTC
We cannot add the may_alias attribute with the current GCC behavior.
Comment 3 Florian Weimer 2016-06-08 09:22:04 UTC
We can try to fix this with a version guard with __GNUC__ >= 7; the relevant test cases should now work.  See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71255.
Comment 4 Florian Weimer 2024-05-07 09:32:15 UTC
Patch posted:

PATCH] socket: Use may_alias on sockaddr structs (bug 19622)
<https://inbox.sourceware.org/libc-alpha/87v83q6kc2.fsf@oldenburg.str.redhat.com/>
Comment 5 Sam James 2024-05-07 11:40:26 UTC
See also https://austingroupbugs.net/view.php?id=1641.
Comment 6 Rich Felker 2024-05-07 19:54:23 UTC
Why is it valid for there to be a previous forward declaration? I don't think it's permitted for applications to make their own declarations of these types.
Comment 7 Florian Weimer 2024-05-18 11:47:43 UTC
Fixed for 2.40 via:

commit 8d7b6b4cb27d4dec1dd5f7960298c1699275f962
Author: Florian Weimer <fweimer@redhat.com>
Date:   Sat May 18 09:33:19 2024 +0200

    socket: Use may_alias on sockaddr structs (bug 19622)
    
    This supports common coding patterns.  The GCC C front end before
    version 7 rejects the may_alias attribute on a struct definition
    if it was not present in a previous forward declaration, so this
    attribute can only be conditionally applied.
    
    This implements the spirit of the change in Austin Group issue 1641.
    
    Suggested-by: Marek Polacek <polacek@redhat.com>
    Suggested-by: Jakub Jelinek <jakub@redhat.com>
    Reviewed-by: Sam James <sam@gentoo.org>
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Comment 8 Florian Weimer 2024-06-13 07:40:54 UTC
*** Bug 30293 has been marked as a duplicate of this bug. ***