You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "Ralf S. Engelschall" <rs...@engelschall.com> on 1998/11/06 12:58:01 UTC

[PATCH] MODULE_MAGIC_COOKIE field for module structure

We have/had PRs (currently especially PR#3152) where users get the following
error message when loading a non-module DSO or at least a garbled one:

| httpd: module "(null)" is not compatible with this version of Apache.
| Please contact the vendor for the correct version.

Here we have two problems: First the "(null)" works only on smart systems and
compilers. On others we can expect a segfault because we print out m->name
which is NULL. Second we do not distinguish between a garbled module DSO (or
even a file which isn't a module DSO at all) and a file which is a module DSO
but is not compatible. For the compatibility checks we have the
MODULE_MAGIC_NUMBER stuff. But just because this doesn't match the current API
version doesn't mean the module is not compatible.  It can also mean the file
we try to load isn't a module DSO at all.

So my idea is the following: We add a "magic" field as the first field of the
module structure. It always contains the value 0x4150414348453133 (=
"APACHE13"). When we load a DSO in mod_so before we use ap_add_loaded_module()
we check this magic field. If it isn't present we can assume we have garbled
DSO file. And only when it is present we call ap_add_loaded_module() which
then itself checks the version, etc. This way (because we do it in mod_so) we
can also give a more useful error message where at least the module name is
included. Because the current error says nothing.

Opinions?
                                       Ralf S. Engelschall
                                       rse@engelschall.com
                                       www.engelschall.com

Index: CHANGES
===================================================================
RCS file: /e/apache/REPOS/apache-1.3/src/CHANGES,v
retrieving revision 1.1136
diff -u -r1.1136 CHANGES
--- CHANGES	1998/11/05 20:11:23	1.1136
+++ CHANGES	1998/11/06 11:49:58
@@ -1,5 +1,10 @@
 Changes with Apache 1.3.4
 
+  *) Added MODULE_MAGIC_COOKIE as the first field in a module structure to
+     allow us to distinguish between a garbled DSO (or even a file which isn't
+     an Apache module DSO at all) and a DSO which doesn't match the current
+     Apache API. [Ralf S. Engelschall] PR#3152
+
   *) Fix problems with handling of UNC names (e.g., \\host\path)
      on Win32.  [Ken Parzygnat <kp...@us.ibm.com>]
 
Index: include/ap_mmn.h
===================================================================
RCS file: /e/apache/REPOS/apache-1.3/src/include/ap_mmn.h,v
retrieving revision 1.9
diff -u -r1.9 ap_mmn.h
--- ap_mmn.h	1998/10/03 18:46:01	1.9
+++ ap_mmn.h	1998/11/06 11:45:53
@@ -181,7 +181,10 @@
  *                        (for implementing better error reporting).
  * 19980906 (1.3.2-dev) - added ap_md5_binary()
  * 19980917 (1.3.2-dev) - bs2000: changed os_set_authfile() to os_set_account()
+ * 19981005 (1.3.4-dev) - Added MODULE_MAGIC_COOKIE to identify module structures
  */
+
+#define MODULE_MAGIC_COOKIE 0x4150414348453133 /* "APACHE13" */
 
 #ifndef MODULE_MAGIC_NUMBER_MAJOR
 #define MODULE_MAGIC_NUMBER_MAJOR 19980917
Index: include/http_config.h
===================================================================
RCS file: /e/apache/REPOS/apache-1.3/src/include/http_config.h,v
retrieving revision 1.96
diff -u -r1.96 http_config.h
--- http_config.h	1998/11/03 13:05:09	1.96
+++ http_config.h	1998/11/06 11:47:07
@@ -186,6 +186,9 @@
  */
 
 typedef struct module_struct {
+    unsigned long magic;        /* Magic Cookie to identify a module;
+                                 * It's especially important in DSO context.
+                                 */
     int version;		/* API version, *not* module version;
 				 * check that module is compatible with this
 				 * version of the server.
@@ -281,7 +284,8 @@
  * signal an error). See src/include/ap_mmn.h for MMN version history.
  */
 
-#define STANDARD_MODULE_STUFF	MODULE_MAGIC_NUMBER_MAJOR, \
+#define STANDARD_MODULE_STUFF	MODULE_MAGIC_COOKIE, \
+				MODULE_MAGIC_NUMBER_MAJOR, \
 				MODULE_MAGIC_NUMBER_MINOR, \
 				-1, \
 				__FILE__, \
Index: modules/standard/mod_so.c
===================================================================
RCS file: /e/apache/REPOS/apache-1.3/src/modules/standard/mod_so.c,v
retrieving revision 1.26
diff -u -r1.26 mod_so.c
--- mod_so.c	1998/09/19 12:27:24	1.26
+++ mod_so.c	1998/11/06 11:47:48
@@ -246,11 +246,21 @@
      * symbol name.
      */
     if (!(modp = (module *)(ap_os_dso_sym(modhandle, modname)))) {
-	return ap_pstrcat(cmd->pool, "Can't find module ", modname,
-		       " in file ", filename, ":", ap_os_dso_error(), NULL);
+	return ap_pstrcat(cmd->pool, "Can't find API module structure named ", modname,
+		       " in file ", szModuleFile, ":", ap_os_dso_error(), NULL);
     }
     modi->modp = modp;
     modp->dynamic_load_handle = modhandle;
+
+    /* 
+     * Make sure the found module structure is really a module structure
+     * 
+     */
+    if (modp->magic != MODULE_MAGIC_COOKIE) {
+        return ap_pstrcat(cmd->pool, "API module structure ", modname,
+                          " in file", szModuleFile, " is garbled -"
+                          " perhaps this is not an Apache module DSO?", NULL);
+    }
 
     /* 
      * Add this module to the Apache core structures

Re: [PATCH] MODULE_MAGIC_COOKIE field for module structure

Posted by Martin Kraemer <ma...@mch.sni.de>.
On Fri, Nov 06, 1998 at 12:58:01PM +0100, Ralf S. Engelschall wrote:
> --- ap_mmn.h	1998/10/03 18:46:01	1.9
> +++ ap_mmn.h	1998/11/06 11:45:53
> @@ -181,7 +181,10 @@
>   *                        (for implementing better error reporting).
>   * 19980906 (1.3.2-dev) - added ap_md5_binary()
>   * 19980917 (1.3.2-dev) - bs2000: changed os_set_authfile() to os_set_account()
> + * 19981005 (1.3.4-dev) - Added MODULE_MAGIC_COOKIE to identify module structures
>   */

You missed Roy's recent WebDAV MMN bump.

> +#define MODULE_MAGIC_COOKIE 0x4150414348453133 /* "APACHE13" */

Can I have some lower case letters here? Or even better: a String?
(That would allow the name to be readable even on EBCDIC platforms).
FWIW, it doesn't have to be a zero-delimited string if the length of
eight matters. Just a thought. OTOH, comparing a long is easier.

BTW, what do you expect the size of a long to be? On most of my
machines, it's still a 4-byte qualtity, only long long (if
supported) is 8 bytes. That would seriously degrade the
"understandability" of your patch...
Even with an appended "UL"
#define MODULE_MAGIC_COOKIE 0x4150414348453133UL /* "APACHE13" */
I still get
/tmp/x.c      4: [warning]:   Integer too big
(and truncation to 4 bytes).

Other than that, I'm +1 on the concept.

    Martin
-- 
<Ma...@Mch.SNI.De>      |        Siemens Information and
Phone: +49-89-636-46021          |        Communication  Products
FAX:   +49-89-636-47816          |        81730  Munich,  Germany