Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13c00ae

Browse files
committed
Fix portability bugs in use of credentials control messages for peer auth.
Even though our existing code for handling credentials control messages hasbeen basically unchanged since 2001, it was fundamentally wrong: it did notensure proper alignment of the supplied buffer, and it was calculatingbuffer sizes and message sizes incorrectly. This led to failures onplatforms where alignment padding is relevant, for instance FreeBSD on64-bit platforms, as seen in a recent Debian bug report passed on byMartin Pitt (http://bugs.debian.org//cgi-bin/bugreport.cgi?bug=612888).Rewrite to do the message-whacking using the macros specified in RFC 2292,following a suggestion from Theo de Raadt in that thread. Tested by meon Debian/kFreeBSD-amd64; since OpenBSD and NetBSD document the identicalCMSG API, it should work there too.Back-patch to all supported branches.
1 parentb4b6923 commit13c00ae

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

‎src/backend/libpq/auth.c

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ auth_peer(hbaPort *port)
17881788
charident_user[IDENT_USERNAME_MAX+1];
17891789

17901790
#if defined(HAVE_GETPEEREID)
1791-
/* OpenBSD style: */
1791+
/* OpenBSD(also Mac OS X)style:use getpeereid() */
17921792
uid_tuid;
17931793
gid_tgid;
17941794
structpasswd*pass;
@@ -1843,7 +1843,7 @@ auth_peer(hbaPort *port)
18431843

18441844
strlcpy(ident_user,pass->pw_name,IDENT_USERNAME_MAX+1);
18451845
#elif defined(HAVE_GETPEERUCRED)
1846-
/* Solaris > 10 */
1846+
/* Solaris > 10: use getpeerucred() */
18471847
uid_tuid;
18481848
structpasswd*pass;
18491849
ucred_t*ucred;
@@ -1878,9 +1878,7 @@ auth_peer(hbaPort *port)
18781878

18791879
strlcpy(ident_user,pass->pw_name,IDENT_USERNAME_MAX+1);
18801880
#elif defined(HAVE_STRUCT_CMSGCRED)|| defined(HAVE_STRUCT_FCRED)|| (defined(HAVE_STRUCT_SOCKCRED)&& defined(LOCAL_CREDS))
1881-
structmsghdrmsg;
1882-
1883-
/* Credentials structure */
1881+
/* Assorted BSDen: use a credentials control message */
18841882
#if defined(HAVE_STRUCT_CMSGCRED)
18851883
typedefstructcmsgcredCred;
18861884

@@ -1894,43 +1892,55 @@ auth_peer(hbaPort *port)
18941892

18951893
#definecruid sc_uid
18961894
#endif
1897-
Cred*cred;
1898-
1899-
/* Compute size without padding */
1900-
charcmsgmem[ALIGN(sizeof(structcmsghdr))+ALIGN(sizeof(Cred))];/* for NetBSD */
1901-
1902-
/* Point to start of first structure */
1903-
structcmsghdr*cmsg= (structcmsghdr*)cmsgmem;
19041895

1896+
structmsghdrmsg;
1897+
structcmsghdr*cmsg;
1898+
union
1899+
{
1900+
structcmsghdrhdr;
1901+
unsignedcharbuf[CMSG_SPACE(sizeof(Cred))];
1902+
}cmsgbuf;
19051903
structioveciov;
19061904
charbuf;
1905+
Cred*cred;
19071906
structpasswd*pw;
19081907

1909-
memset(&msg,0,sizeof(msg));
1910-
msg.msg_iov=&iov;
1911-
msg.msg_iovlen=1;
1912-
msg.msg_control= (char*)cmsg;
1913-
msg.msg_controllen=sizeof(cmsgmem);
1914-
memset(cmsg,0,sizeof(cmsgmem));
1915-
19161908
/*
1917-
* The one characterwhich is received here is not meaningful; its
1918-
*purposesis only to make sure that recvmsg() blocks long enough for the
1919-
*otherside to send its credentials.
1909+
* The one characterthat is received here is not meaningful; its purpose
1910+
* is only to make sure that recvmsg() blocks long enough for the other
1911+
* side to send its credentials.
19201912
*/
19211913
iov.iov_base=&buf;
19221914
iov.iov_len=1;
19231915

1924-
if (recvmsg(port->sock,&msg,0)<0||
1925-
cmsg->cmsg_len<sizeof(cmsgmem)||
1926-
cmsg->cmsg_type!=SCM_CREDS)
1916+
memset(&msg,0,sizeof(msg));
1917+
msg.msg_iov=&iov;
1918+
msg.msg_iovlen=1;
1919+
msg.msg_control=&cmsgbuf.buf;
1920+
msg.msg_controllen=sizeof(cmsgbuf.buf);
1921+
memset(&cmsgbuf,0,sizeof(cmsgbuf));
1922+
1923+
if (recvmsg(port->sock,&msg,0)<0)
19271924
{
19281925
ereport(LOG,
19291926
(errcode_for_socket_access(),
19301927
errmsg("could not get peer credentials: %m")));
19311928
returnSTATUS_ERROR;
19321929
}
19331930

1931+
cmsg=CMSG_FIRSTHDR(&msg);
1932+
if (msg.msg_flags& (MSG_TRUNC |MSG_CTRUNC)||
1933+
cmsg==NULL||
1934+
cmsg->cmsg_len<CMSG_LEN(sizeof(Cred))||
1935+
cmsg->cmsg_level!=SOL_SOCKET||
1936+
cmsg->cmsg_type!=SCM_CREDS)
1937+
{
1938+
ereport(LOG,
1939+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
1940+
errmsg("could not get peer credentials: incorrect control message")));
1941+
returnSTATUS_ERROR;
1942+
}
1943+
19341944
cred= (Cred*)CMSG_DATA(cmsg);
19351945

19361946
pw=getpwuid(cred->cruid);

‎src/interfaces/libpq/fe-auth.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,12 @@ pg_local_sendauth(PGconn *conn)
693693
structmsghdrmsg;
694694

695695
#ifdefHAVE_STRUCT_CMSGCRED
696-
/* Prevent padding */
697-
charcmsgmem[sizeof(structcmsghdr)+sizeof(structcmsgcred)];
698-
699-
/* Point to start of first structure */
700-
structcmsghdr*cmsg= (structcmsghdr*)cmsgmem;
696+
structcmsghdr*cmsg;
697+
union
698+
{
699+
structcmsghdrhdr;
700+
unsignedcharbuf[CMSG_SPACE(sizeof(structcmsgcred))];
701+
}cmsgbuf;
701702
#endif
702703

703704
/*
@@ -713,11 +714,12 @@ pg_local_sendauth(PGconn *conn)
713714
msg.msg_iovlen=1;
714715

715716
#ifdefHAVE_STRUCT_CMSGCRED
716-
/* Create control header, FreeBSD */
717-
msg.msg_control=cmsg;
718-
msg.msg_controllen=sizeof(cmsgmem);
719-
memset(cmsg,0,sizeof(cmsgmem));
720-
cmsg->cmsg_len=sizeof(cmsgmem);
717+
/* FreeBSD needs us to set up a message that will be filled in by kernel */
718+
memset(&cmsgbuf,0,sizeof(cmsgbuf));
719+
msg.msg_control=&cmsgbuf.buf;
720+
msg.msg_controllen=sizeof(cmsgbuf.buf);
721+
cmsg=CMSG_FIRSTHDR(&msg);
722+
cmsg->cmsg_len=CMSG_LEN(sizeof(structcmsgcred));
721723
cmsg->cmsg_level=SOL_SOCKET;
722724
cmsg->cmsg_type=SCM_CREDS;
723725
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp