Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/compat/mach - When MACH_MSG_RCV_LARGE is set, we must re...



details:   https://anonhg.NetBSD.org/src/rev/fd992cd553ce
branches:  trunk
changeset: 540889:fd992cd553ce
user:      manu <manu%NetBSD.org@localhost>
date:      Fri Dec 27 19:57:47 2002 +0000

description:
- When MACH_MSG_RCV_LARGE is set, we must return a message with just a header
and a body.
- If mach_init is not availabkle for boostrap requests, try to handle them
in the kernel (we don't really handle them, we just try to avoid hanging there)
- minor tweaks.

diffstat:

 sys/compat/mach/mach_clock.c   |  13 +-------
 sys/compat/mach/mach_clock.h   |   5 +--
 sys/compat/mach/mach_errno.c   |   7 +++-
 sys/compat/mach/mach_exec.c    |  11 +++---
 sys/compat/mach/mach_message.c |  66 ++++++++++++++++++++++++++---------------
 sys/compat/mach/mach_message.h |   7 +++-
 sys/compat/mach/mach_port.c    |  14 +++++++-
 sys/compat/mach/mach_port.h    |   4 +-
 sys/compat/mach/mach_task.c    |   9 ++---
 9 files changed, 79 insertions(+), 57 deletions(-)

diffs (truncated from 403 to 300 lines):

diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_clock.c
--- a/sys/compat/mach/mach_clock.c      Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_clock.c      Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_clock.c,v 1.6 2002/12/26 13:45:17 manu Exp $ */
+/*     $NetBSD: mach_clock.c,v 1.7 2002/12/27 19:57:47 manu Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mach_clock.c,v 1.6 2002/12/26 13:45:17 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mach_clock.c,v 1.7 2002/12/27 19:57:47 manu Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -53,8 +53,6 @@
 #include <compat/mach/mach_clock.h>
 #include <compat/mach/mach_syscallargs.h>
 
-struct mach_port *mach_clock_port;
-
 int
 mach_sys_clock_sleep_trap(p, v, retval)
        struct proc *p;
@@ -154,10 +152,3 @@
 
        return 0;
 }
-
-void
-mach_clock_init(void)
-{
-       mach_clock_port = mach_port_get();
-       return;
-}
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_clock.h
--- a/sys/compat/mach/mach_clock.h      Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_clock.h      Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_clock.h,v 1.4 2002/12/26 13:45:18 manu Exp $ */
+/*     $NetBSD: mach_clock.h,v 1.5 2002/12/27 19:57:47 manu Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -68,9 +68,6 @@
        mach_msg_trailer_t rep_trailer;
 } mach_clock_get_time_reply_t;
 
-extern struct mach_port *mach_clock_port;
-
 int mach_clock_get_time(struct mach_trap_args *);
-void mach_clock_init(void);
 
 #endif /* _MACH_CLOCK_H_ */
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_errno.c
--- a/sys/compat/mach/mach_errno.c      Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_errno.c      Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_errno.c,v 1.10 2002/12/24 15:54:26 manu Exp $ */
+/*     $NetBSD: mach_errno.c,v 1.11 2002/12/27 19:57:47 manu Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mach_errno.c,v 1.10 2002/12/24 15:54:26 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mach_errno.c,v 1.11 2002/12/27 19:57:47 manu Exp $");
 
 #include <sys/types.h>
 #include <sys/systm.h>
@@ -157,5 +157,8 @@
 
        *msglen = sizeof(*rep);
 
+#ifdef DEBUG_MACH
+       printf("failure in kernel handler for msg id %d\n", req->msgh_id);
+#endif
        return 0;
 }
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_exec.c
--- a/sys/compat/mach/mach_exec.c       Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_exec.c       Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_exec.c,v 1.19 2002/12/27 09:59:25 manu Exp $       */
+/*     $NetBSD: mach_exec.c,v 1.20 2002/12/27 19:57:47 manu Exp $       */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.19 2002/12/27 09:59:25 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.20 2002/12/27 19:57:47 manu Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,7 +55,6 @@
 #include <compat/mach/mach_types.h>
 #include <compat/mach/mach_message.h>
 #include <compat/mach/mach_port.h>
-#include <compat/mach/mach_clock.h>
 #include <compat/mach/mach_semaphore.h>
 #include <compat/mach/mach_exec.h>
 
@@ -241,17 +240,18 @@
 
        LIST_INIT(&med->med_right);
 
-       med->med_bootstrap = mach_port_get();
        med->med_kernel = mach_port_get();
        med->med_host = mach_port_get();
        med->med_exception = mach_port_get();
 
        /* Make sure they will not be deallocated */
-       med->med_bootstrap->mp_refcount++;
        med->med_kernel->mp_refcount++;
        med->med_host->mp_refcount++;
        med->med_exception->mp_refcount++;
 
+       med->med_bootstrap = mach_bootstrap_port;
+       med->med_bootstrap->mp_refcount++;
+
        return;
 }
 
@@ -292,7 +292,6 @@
        mach_semaphore_init();
        mach_message_init();
        mach_port_init();
-       mach_clock_init();
 
        mach_cold = 0;
 
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_message.c
--- a/sys/compat/mach/mach_message.c    Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_message.c    Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_message.c,v 1.12 2002/12/27 09:59:26 manu Exp $ */
+/*     $NetBSD: mach_message.c,v 1.13 2002/12/27 19:57:47 manu Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mach_message.c,v 1.12 2002/12/27 09:59:26 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mach_message.c,v 1.13 2002/12/27 19:57:47 manu Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_compat_mach.h" /* For COMPAT_MACH in <sys/ktrace.h> */
@@ -86,29 +86,24 @@
                syscallarg(mach_msg_size_t) scatter_list_size;
        } */ *uap = v;
        struct mach_emuldata *med;
-       size_t send_size, rcv_size;
        struct mach_port *mp;
        struct mach_right *mr;
+       size_t send_size, rcv_size;
        int error = 0;
 
-       /*
-        * If neither send nor recieve, do nothing.
-        */
-       if (SCARG(uap, option) & ~(MACH_SEND_MSG | MACH_RCV_MSG)) 
-               return 0;
-
-       /* 
-        * XXX Sanity check on the message size. This is not an accurate
-        * emulation, since Mach messages can be as large as 4GB. 
-        * Additionnaly, this does not address DoS attack by queueing
-        * lots of big messages in the kernel.
-        */
        send_size = SCARG(uap, send_size);
        rcv_size = SCARG(uap, rcv_size);
-       if ((send_size > MACH_MAX_MSG_LEN) || (rcv_size > MACH_MAX_MSG_LEN)) {
+
+       /* XXX not safe enough: lots of big messages will kill us */
+       if (send_size > MACH_MAX_MSG_LEN) {
                *retval = MACH_SEND_TOO_LARGE;
                return 0;
        }
+       if (rcv_size > MACH_MAX_MSG_LEN) {
+               *retval = MACH_RCV_TOO_LARGE;
+               return 0;
+       }
+
 
        /* 
         * Two options: receive or send. If both are 
@@ -177,9 +172,8 @@
                 */
                med = (struct mach_emuldata *)p->p_emuldata;
                mp = rr->mr_port;
-               if ((mp == med->med_host) || 
-                   (mp == med->med_kernel) ||
-                   (mp == mach_clock_port)) {
+               if ((mp == med->med_host) || (mp == med->med_kernel) ||
+                   (mp == mach_clock_port) || (mp == mach_bootstrap_port)) {
                        struct mach_trap_args args;
                        mach_msg_header_t *rm;
 
@@ -224,14 +218,17 @@
                        if ((*retval = (*map->map_handler)(&args)) != 0) 
                                goto out3;
                        
+#ifdef DEBUG_MACH
                        /* 
                         * Catch potential bug in the handler
                         */
-                       if (rcv_size > SCARG(uap, rcv_size)) {
+                       if ((SCARG(uap, option) & MACH_RCV_MSG) &&
+                           (rcv_size > SCARG(uap, rcv_size))) {
                                uprintf("mach_msg: reply too big in %s\n",
                                    map->map_name);
                                rcv_size = SCARG(uap, rcv_size);
                        }
+#endif
 
                        /*
                         * Queue the reply
@@ -441,16 +438,37 @@
 #endif
 
                if (mm->mm_size > rcv_size) {
+                       struct mach_short_reply sr;
+
+                       *retval = MACH_RCV_TOO_LARGE;
                        /* 
-                        * If MACH_RCV_LARGE was not set, destroy the
-                        * message. If it was set, just notice that 
-                        * the message is too big.
+                        * If MACH_RCV_LARGE was not set, destroy the message.
                         */
                        if ((SCARG(uap, option) & MACH_RCV_LARGE) == 0) {
                                free(mm->mm_msg, M_EMULDATA);
                                mach_message_put_shlocked(mm);
+                               goto unlock;
                        }               
-                       *retval = MACH_RCV_TOO_LARGE;
+
+                       /* 
+                        * If MACH_RCV_TOO_LARGE is set, then return 
+                        * a message with just header and trailer. The 
+                        * size in the header should correspond to the
+                        * whole message, so just copy the whole header.
+                        */
+                       memcpy(&sr, mm->mm_msg, sizeof(mach_msg_header_t));
+                       sr.sr_trailer.msgh_trailer_type = 0;
+                       sr.sr_trailer.msgh_trailer_size = 8;
+
+                       if ((error = copyout(&sr, urm, sizeof(sr))) != 0) {
+                               *retval = MACH_RCV_INVALID_DATA;
+                               goto unlock;
+                       }
+#ifdef KTRACE
+                       /* Dump the Mach message */
+                       if (KTRPOINT(p, KTR_MMSG))
+                               ktrmmsg(p, (char *)&sr, sizeof(sr)); 
+#endif
                        goto unlock;
                }
 
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_message.h
--- a/sys/compat/mach/mach_message.h    Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_message.h    Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mach_message.h,v 1.10 2002/12/24 15:54:26 manu Exp $    */
+/*     $NetBSD: mach_message.h,v 1.11 2002/12/27 19:57:47 manu Exp $    */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -197,12 +197,17 @@
        mach_msg_size_t msgh_descriptor_count;
 } mach_msg_body_t;
 
+struct mach_short_reply {
+       mach_msg_header_t sr_header;
+       mach_msg_trailer_t sr_trailer;
+};
 struct mach_trap_args {
        struct proc *p;
        void *smsg;
        void *rmsg;
        size_t *rsize;
 };
+
 struct mach_subsystem_namemap {
        int     map_id;
        int     (*map_handler)(struct mach_trap_args *);
diff -r 4f05c0c3bc1e -r fd992cd553ce sys/compat/mach/mach_port.c
--- a/sys/compat/mach/mach_port.c       Fri Dec 27 19:54:54 2002 +0000
+++ b/sys/compat/mach/mach_port.c       Fri Dec 27 19:57:47 2002 +0000
@@ -1,4 +1,4 @@



Home | Main Index | Thread Index | Old Index