* winsup.h (__check_null_invalid_struct): Make ptr argument non-const.

(__check_null_invalid_struct_errno): Ditto.
	* miscfuncs.cc (__check_null_invalid_struct): Ditto.
	(__check_null_invalid_struct_errno): Ditto.
	(__check_invalid_read_ptr_errno): Remove superfluous cast.
	* net.cc (get): Set appropriate errno if fd is not a socket.
	(cygwin_sendto): Fix parameter checking.
	(cygwin_recvfrom): Ditto.
	(cygwin_setsockopt): Ditto.
	(cygwin_getsockopt): Ditto.
	(cygwin_connect): Ditto.
	(cygwin_gethostbyaddr): Ditto.
	(cygwin_accept): Ditto.
	(cygwin_bind): Ditto.
	(cygwin_getsockname): Ditto.
	(cygwin_listen): Ditto.
	(cygwin_getpeername): Ditto.
	(cygwin_send): Ditto.
	(cygwin_shutdown): Ditto.  Move sigframe to fhandler_socket.
	(cygwin_recvmsg): Fix parameter checking.  Add tracing.
	(cygwin_sendmsg): Ditto.
	* fhandler_socket.cc (fhandler_socket::shutdown): Add sigframe.
	* resource.cc (setrlimit): Fix parameter checking.
This commit is contained in:
Conrad Scott 2002-08-08 17:03:20 +00:00
parent b7e664540b
commit b4f06520f4
6 changed files with 192 additions and 151 deletions

View File

@ -1,3 +1,29 @@
2002-08-07 Conrad Scott <conrad.scott@dsl.pipex.com>
* winsup.h (__check_null_invalid_struct): Make ptr argument non-const.
(__check_null_invalid_struct_errno): Ditto.
* miscfuncs.cc (__check_null_invalid_struct): Ditto.
(__check_null_invalid_struct_errno): Ditto.
(__check_invalid_read_ptr_errno): Remove superfluous cast.
* net.cc (get): Set appropriate errno if fd is not a socket.
(cygwin_sendto): Fix parameter checking.
(cygwin_recvfrom): Ditto.
(cygwin_setsockopt): Ditto.
(cygwin_getsockopt): Ditto.
(cygwin_connect): Ditto.
(cygwin_gethostbyaddr): Ditto.
(cygwin_accept): Ditto.
(cygwin_bind): Ditto.
(cygwin_getsockname): Ditto.
(cygwin_listen): Ditto.
(cygwin_getpeername): Ditto.
(cygwin_send): Ditto.
(cygwin_shutdown): Ditto. Move sigframe to fhandler_socket.
(cygwin_recvmsg): Fix parameter checking. Add tracing.
(cygwin_sendmsg): Ditto.
* fhandler_socket.cc (fhandler_socket::shutdown): Add sigframe.
* resource.cc (setrlimit): Fix parameter checking.
2002-08-08 Joe Buehler <jbuehler@hekimian.com 2002-08-08 Joe Buehler <jbuehler@hekimian.com
* sec_helper.cc (sec_acl): remove extraneous arg to debug_printf. * sec_helper.cc (sec_acl): remove extraneous arg to debug_printf.

View File

@ -915,6 +915,8 @@ fhandler_socket::sendmsg (const struct msghdr *msg, int flags)
int int
fhandler_socket::shutdown (int how) fhandler_socket::shutdown (int how)
{ {
sigframe thisframe (mainthread);
int res = ::shutdown (get_socket (), how); int res = ::shutdown (get_socket (), how);
if (res) if (res)

View File

@ -154,16 +154,16 @@ check_null_str_errno (const char *name)
} }
int __stdcall int __stdcall
__check_null_invalid_struct (const void *s, unsigned sz) __check_null_invalid_struct (void *s, unsigned sz)
{ {
if (s && !IsBadWritePtr ((void *) s, sz)) if (s && !IsBadWritePtr (s, sz))
return 0; return 0;
return EFAULT; return EFAULT;
} }
int __stdcall int __stdcall
__check_null_invalid_struct_errno (const void *s, unsigned sz) __check_null_invalid_struct_errno (void *s, unsigned sz)
{ {
int err; int err;
if ((err = __check_null_invalid_struct (s, sz))) if ((err = __check_null_invalid_struct (s, sz)))
@ -174,7 +174,7 @@ __check_null_invalid_struct_errno (const void *s, unsigned sz)
int __stdcall int __stdcall
__check_invalid_read_ptr_errno (const void *s, unsigned sz) __check_invalid_read_ptr_errno (const void *s, unsigned sz)
{ {
if (s && !IsBadReadPtr ((void *) s, sz)) if (s && !IsBadReadPtr (s, sz))
return 0; return 0;
return set_errno (EFAULT); return set_errno (EFAULT);
} }

View File

@ -101,13 +101,17 @@ WSADATA wsadata;
/* Cygwin internal */ /* Cygwin internal */
static fhandler_socket * static fhandler_socket *
get (int fd) get (const int fd)
{ {
cygheap_fdget cfd (fd); cygheap_fdget cfd (fd);
if (cfd < 0) if (cfd < 0)
return 0; return 0;
return cfd->is_socket (); fhandler_socket *const fh = cfd->is_socket ();
if (!fh)
set_errno (ENOTSOCK);
return fh;
} }
/* Cygwin internal */ /* Cygwin internal */
@ -567,7 +571,7 @@ cygwin_sendto (int fd, const void *buf, int len, unsigned int flags,
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if ((len && __check_invalid_read_ptr_errno (buf, (unsigned) len)) if ((len && __check_invalid_read_ptr_errno (buf, (unsigned) len))
|| __check_null_invalid_struct_errno (to, tolen) || (to &&__check_invalid_read_ptr_errno (to, tolen))
|| !fh) || !fh)
res = -1; res = -1;
else else
@ -587,8 +591,9 @@ cygwin_recvfrom (int fd, char *buf, int len, int flags, struct sockaddr *from,
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (__check_null_invalid_struct_errno (buf, (unsigned) len) if (__check_null_invalid_struct_errno (buf, (unsigned) len)
|| check_null_invalid_struct_errno (fromlen) || (from
|| (from && __check_null_invalid_struct_errno (from, (unsigned) *fromlen)) && (check_null_invalid_struct_errno (fromlen)
||__check_null_invalid_struct_errno (from, (unsigned) *fromlen)))
|| !fh) || !fh)
res = -1; res = -1;
else else
@ -604,12 +609,10 @@ extern "C" int
cygwin_setsockopt (int fd, int level, int optname, const void *optval, cygwin_setsockopt (int fd, int level, int optname, const void *optval,
int optlen) int optlen)
{ {
int res;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
int res = -1;
const char *name = "error"; const char *name = "error";
if ((!optval || !__check_invalid_read_ptr_errno (optval, optlen)) && fh)
{
/* For the following debug_printf */ /* For the following debug_printf */
switch (optname) switch (optname)
{ {
@ -645,6 +648,10 @@ cygwin_setsockopt (int fd, int level, int optname, const void *optval,
break; break;
} }
if ((optval && __check_invalid_read_ptr_errno (optval, optlen)) || !fh)
res = -1;
else
{
res = setsockopt (fh->get_socket (), level, optname, res = setsockopt (fh->get_socket (), level, optname,
(const char *) optval, optlen); (const char *) optval, optlen);
@ -664,14 +671,10 @@ cygwin_setsockopt (int fd, int level, int optname, const void *optval,
extern "C" int extern "C" int
cygwin_getsockopt (int fd, int level, int optname, void *optval, int *optlen) cygwin_getsockopt (int fd, int level, int optname, void *optval, int *optlen)
{ {
int res;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
int res = -1;
const char *name = "error"; const char *name = "error";
if (!check_null_invalid_struct_errno (optlen)
&& (!optval
|| !__check_null_invalid_struct_errno (optval, (unsigned) *optlen))
&& fh)
{
/* For the following debug_printf */ /* For the following debug_printf */
switch (optname) switch (optname)
{ {
@ -707,6 +710,13 @@ cygwin_getsockopt (int fd, int level, int optname, void *optval, int *optlen)
break; break;
} }
if ((optval
&& (check_null_invalid_struct_errno (optlen)
|| __check_null_invalid_struct_errno (optval, (unsigned) *optlen)))
|| !fh)
res = -1;
else
{
res = getsockopt (fh->get_socket (), level, optname, (char *) optval, res = getsockopt (fh->get_socket (), level, optname, (char *) optval,
(int *) optlen); (int *) optlen);
@ -732,10 +742,7 @@ cygwin_connect (int fd, const struct sockaddr *name, int namelen)
int res; int res;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (__check_invalid_read_ptr_errno (name, namelen)) if (__check_invalid_read_ptr_errno (name, namelen) || !fh)
return -1;
if (!fh)
res = -1; res = -1;
else else
res = fh->connect (name, namelen); res = fh->connect (name, namelen);
@ -970,7 +977,7 @@ cygwin_gethostbyname (const char *name)
extern "C" struct hostent * extern "C" struct hostent *
cygwin_gethostbyaddr (const char *addr, int len, int type) cygwin_gethostbyaddr (const char *addr, int len, int type)
{ {
if (__check_null_invalid_struct_errno (addr, len)) if (__check_invalid_read_ptr_errno (addr, len))
return NULL; return NULL;
free_hostent_ptr (hostent_buf); free_hostent_ptr (hostent_buf);
@ -992,15 +999,14 @@ cygwin_gethostbyaddr (const char *addr, int len, int type)
extern "C" int extern "C" int
cygwin_accept (int fd, struct sockaddr *peer, int *len) cygwin_accept (int fd, struct sockaddr *peer, int *len)
{ {
if (peer != NULL int res;
&& (check_null_invalid_struct_errno (len)
|| __check_null_invalid_struct_errno (peer, (unsigned) *len)))
return -1;
int res = -1;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (fh)
if ((peer && (check_null_invalid_struct_errno (len)
|| __check_null_invalid_struct_errno (peer, (unsigned) *len)))
|| !fh)
res = -1;
else
res = fh->accept (peer, len); res = fh->accept (peer, len);
syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len); syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len);
@ -1011,13 +1017,12 @@ cygwin_accept (int fd, struct sockaddr *peer, int *len)
extern "C" int extern "C" int
cygwin_bind (int fd, const struct sockaddr *my_addr, int addrlen) cygwin_bind (int fd, const struct sockaddr *my_addr, int addrlen)
{ {
if (__check_null_invalid_struct_errno (my_addr, addrlen)) int res;
return -1;
int res = -1;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (fh)
if (__check_invalid_read_ptr_errno (my_addr, addrlen) || !fh)
res = -1;
else
res = fh->bind (my_addr, addrlen); res = fh->bind (my_addr, addrlen);
syscall_printf ("%d = bind (%d, %x, %d)", res, fd, my_addr, addrlen); syscall_printf ("%d = bind (%d, %x, %d)", res, fd, my_addr, addrlen);
@ -1028,14 +1033,14 @@ cygwin_bind (int fd, const struct sockaddr *my_addr, int addrlen)
extern "C" int extern "C" int
cygwin_getsockname (int fd, struct sockaddr *addr, int *namelen) cygwin_getsockname (int fd, struct sockaddr *addr, int *namelen)
{ {
if (check_null_invalid_struct_errno (namelen) int res;
|| __check_null_invalid_struct_errno (addr, (unsigned) *namelen))
return -1;
int res = -1;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (fh)
if (check_null_invalid_struct_errno (namelen)
|| __check_null_invalid_struct_errno (addr, (unsigned) *namelen)
|| !fh)
res = -1;
else
res = fh->getsockname (addr, namelen); res = fh->getsockname (addr, namelen);
syscall_printf ("%d = getsockname (%d, %x, %d)", res, fd, addr, namelen); syscall_printf ("%d = getsockname (%d, %x, %d)", res, fd, addr, namelen);
@ -1046,10 +1051,12 @@ cygwin_getsockname (int fd, struct sockaddr *addr, int *namelen)
extern "C" int extern "C" int
cygwin_listen (int fd, int backlog) cygwin_listen (int fd, int backlog)
{ {
int res = -1; int res;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (fh)
if (!fh)
res = -1;
else
res = fh->listen (backlog); res = fh->listen (backlog);
syscall_printf ("%d = listen (%d, %d)", res, fd, backlog); syscall_printf ("%d = listen (%d, %d)", res, fd, backlog);
@ -1060,11 +1067,12 @@ cygwin_listen (int fd, int backlog)
extern "C" int extern "C" int
cygwin_shutdown (int fd, int how) cygwin_shutdown (int fd, int how)
{ {
int res = -1; int res;
sigframe thisframe (mainthread);
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (fh)
if (!fh)
res = -1;
else
res = fh->shutdown (how); res = fh->shutdown (how);
syscall_printf ("%d = shutdown (%d, %d)", res, fd, how); syscall_printf ("%d = shutdown (%d, %d)", res, fd, how);
@ -1122,18 +1130,17 @@ cygwin_herror (const char *s)
extern "C" int extern "C" int
cygwin_getpeername (int fd, struct sockaddr *name, int *len) cygwin_getpeername (int fd, struct sockaddr *name, int *len)
{ {
int res = -1; int res;
fhandler_socket *fh = get (fd);
if (check_null_invalid_struct_errno (len) if (check_null_invalid_struct_errno (len)
|| __check_null_invalid_struct_errno (name, (unsigned) *len)) || __check_null_invalid_struct_errno (name, (unsigned) *len)
return -1; || !fh)
res = -1;
fhandler_socket *fh = get (fd); else
if (fh)
res = fh->getpeername (name, len); res = fh->getpeername (name, len);
syscall_printf ("%d = getpeername %d", res, (fh ? fh->get_socket () : -1)); syscall_printf ("%d = getpeername %d", res, (fh ? fh->get_socket () : -1));
return res; return res;
} }
@ -1150,7 +1157,6 @@ cygwin_recv (int fd, void *buf, int len, unsigned int flags)
res = fh->recv (buf, len, flags); res = fh->recv (buf, len, flags);
syscall_printf ("%d = recv (%d, %x, %x, %x)", res, fd, buf, len, flags); syscall_printf ("%d = recv (%d, %x, %x, %x)", res, fd, buf, len, flags);
return res; return res;
} }
@ -1161,13 +1167,12 @@ cygwin_send (int fd, const void *buf, int len, unsigned int flags)
int res; int res;
fhandler_socket *fh = get (fd); fhandler_socket *fh = get (fd);
if (__check_invalid_read_ptr_errno (buf, len) || !fh) if ((len &&__check_invalid_read_ptr_errno (buf, len)) || !fh)
res = -1; res = -1;
else else
res = fh->send (buf, len, flags); res = fh->send (buf, len, flags);
syscall_printf ("%d = send (%d, %x, %d, %x)", res, fd, buf, len, flags); syscall_printf ("%d = send (%d, %x, %d, %x)", res, fd, buf, len, flags);
return res; return res;
} }
@ -2095,32 +2100,40 @@ endhostent (void)
/* exported as recvmsg: standards? */ /* exported as recvmsg: standards? */
extern "C" int extern "C" int
cygwin_recvmsg (int s, struct msghdr *msg, int flags) cygwin_recvmsg (int fd, struct msghdr *msg, int flags)
{ {
if (check_null_invalid_struct_errno (msg)) int res;
return -1; fhandler_socket *fh = get (fd);
fhandler_socket *fh = get (s); if (check_null_invalid_struct_errno (msg)
if (!fh) || (msg->msg_name
{ && __check_null_invalid_struct_errno (msg->msg_name,
set_errno (EINVAL); (unsigned) msg->msg_namelen))
return -1; || !fh)
} res = -1;
return fh->recvmsg (msg, flags); else
res = fh->recvmsg (msg, flags);
syscall_printf ("%d = recvmsg (%d, %x, %x)", res, fd, msg, flags);
return res;
} }
/* exported as sendmsg: standards? */ /* exported as sendmsg: standards? */
extern "C" int extern "C" int
cygwin_sendmsg (int s, const struct msghdr *msg, int flags) cygwin_sendmsg (int fd, const struct msghdr *msg, int flags)
{ {
if (__check_invalid_read_ptr_errno (msg, sizeof msg)) int res;
return -1; fhandler_socket *fh = get (fd);
fhandler_socket *fh = get (s); if (__check_invalid_read_ptr_errno (msg, sizeof msg)
if (!fh) || (msg->msg_name
{ && __check_invalid_read_ptr_errno (msg->msg_name,
set_errno (EINVAL); (unsigned) msg->msg_namelen))
return -1; || !fh)
} res = -1;
return fh->sendmsg (msg, flags); else
res = fh->sendmsg (msg, flags);
syscall_printf ("%d = recvmsg (%d, %x, %x)", res, fd, msg, flags);
return res;
} }

View File

@ -157,7 +157,7 @@ getrlimit (int resource, struct rlimit *rlp)
extern "C" int extern "C" int
setrlimit (int resource, const struct rlimit *rlp) setrlimit (int resource, const struct rlimit *rlp)
{ {
if (check_null_invalid_struct_errno (rlp)) if (__check_invalid_read_ptr_errno (rlp, sizeof (*rlp)))
return -1; return -1;
struct rlimit oldlimits; struct rlimit oldlimits;

View File

@ -212,8 +212,8 @@ int __stdcall check_null_str (const char *name) __attribute__ ((regparm(1)));
int __stdcall check_null_empty_str (const char *name) __attribute__ ((regparm(1))); int __stdcall check_null_empty_str (const char *name) __attribute__ ((regparm(1)));
int __stdcall check_null_empty_str_errno (const char *name) __attribute__ ((regparm(1))); int __stdcall check_null_empty_str_errno (const char *name) __attribute__ ((regparm(1)));
int __stdcall check_null_str_errno (const char *name) __attribute__ ((regparm(1))); int __stdcall check_null_str_errno (const char *name) __attribute__ ((regparm(1)));
int __stdcall __check_null_invalid_struct (const void *s, unsigned sz) __attribute__ ((regparm(2))); int __stdcall __check_null_invalid_struct (void *s, unsigned sz) __attribute__ ((regparm(2)));
int __stdcall __check_null_invalid_struct_errno (const void *s, unsigned sz) __attribute__ ((regparm(2))); int __stdcall __check_null_invalid_struct_errno (void *s, unsigned sz) __attribute__ ((regparm(2)));
int __stdcall __check_invalid_read_ptr_errno (const void *s, unsigned sz) __attribute__ ((regparm(2))); int __stdcall __check_invalid_read_ptr_errno (const void *s, unsigned sz) __attribute__ ((regparm(2)));
#define check_null_invalid_struct(s) \ #define check_null_invalid_struct(s) \