Source-Changes-HG archive

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

[src/trunk]: src/share/examples/secmodel Update secmodel_examples to better d...



details:   https://anonhg.NetBSD.org/src/rev/7a118dad44d1
branches:  trunk
changeset: 771871:7a118dad44d1
user:      jym <jym%NetBSD.org@localhost>
date:      Sun Dec 04 23:55:36 2011 +0000

description:
Update secmodel_examples to better describe the secmodel(9) API.

diffstat:

 share/examples/secmodel/example.h          |    6 +-
 share/examples/secmodel/secmodel_example.c |  369 ++++++++++++++++++++++++----
 2 files changed, 319 insertions(+), 56 deletions(-)

diffs (truncated from 563 to 300 lines):

diff -r bf0dba1a4664 -r 7a118dad44d1 share/examples/secmodel/example.h
--- a/share/examples/secmodel/example.h Sun Dec 04 23:13:54 2011 +0000
+++ b/share/examples/secmodel/example.h Sun Dec 04 23:55:36 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: example.h,v 1.1 2006/09/15 15:49:29 elad Exp $ */
+/* $NetBSD: example.h,v 1.2 2011/12/04 23:55:36 jym Exp $ */
 
 /* 
  * This file is placed in the public domain.
@@ -7,7 +7,7 @@
 #ifndef _SECMODEL_EXAMPLE_EXAMPLE_H_
 #define        _SECMODEL_EXAMPLE_EXAMPLE_H_
 
-void secmodel_example_init(void);
-void secmodel_example_start(void);
+#define SECMODEL_EXAMPLE_ID   "id.unique.secmodel.example"
+#define SECMODEL_EXAMPLE_NAME "Example security model"
 
 #endif /* !_SECMODEL_EXAMPLE_EXAMPLE_H_ */
diff -r bf0dba1a4664 -r 7a118dad44d1 share/examples/secmodel/secmodel_example.c
--- a/share/examples/secmodel/secmodel_example.c        Sun Dec 04 23:13:54 2011 +0000
+++ b/share/examples/secmodel/secmodel_example.c        Sun Dec 04 23:55:36 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: secmodel_example.c,v 1.25 2008/02/28 17:07:49 elad Exp $ */
+/* $NetBSD: secmodel_example.c,v 1.26 2011/12/04 23:55:36 jym Exp $ */
 
 /*
  * This file is placed in the public domain.
@@ -13,42 +13,52 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: secmodel_example.c,v 1.25 2008/02/28 17:07:49 elad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: secmodel_example.c,v 1.26 2011/12/04 23:55:36 jym Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/kauth.h>
 
+#include <sys/module.h>
 #include <sys/sysctl.h>
 
 #include <secmodel/secmodel.h>
-
 #include <secmodel/example/example.h>
 
-/*
- * Initialize the security model.
- */
-void
-secmodel_example_init(void)
-{
-       return;
-}
+MODULE(MODULE_CLASS_SECMODEL, secmodel_example, NULL);
+
+static secmodel_t example_sm;
+static struct sysctllog *sysctl_example_log;
+
+static kauth_listener_t l_device, l_generic, l_machdep, l_network,
+    l_process, l_system, l_vnode;
+
+static void secmodel_example_init(void);
+static void secmodel_example_start(void);
+static void secmodel_example_stop(void);
+
+static void sysctl_security_example_setup(struct sysctllog **);
+
+static int secmodel_example_device_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_generic_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_machdep_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_network_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_process_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_system_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
+static int secmodel_example_vnode_cb(kauth_cred_t, kauth_action_t, void *,
+    void *, void *, void *, void *);
 
 /*
- * If the security model is to be used as an LKM, this routine should be
- * changed, because otherwise creating permanent sysctl(9) nodes will fail.
- *
- * To make it work, the prototype should be changed to something like:
- *
- *     void secmodel_example_sysctl(void)
- *
- * and it should be called from secmodel_start().
- *
- * In addition, the CTLFLAG_PERMANENT flag must be removed from all the
- * nodes.
+ * Creates sysctl(7) entries expected from a security model.
  */
-SYSCTL_SETUP(sysctl_security_example_setup,
-    "sysctl security example setup")
+static void
+sysctl_security_example_setup(struct sysctllog **clog)
 {
        const struct sysctlnode *rnode;
 
@@ -74,37 +84,131 @@
        sysctl_createv(clog, 0, &rnode, NULL,
                       CTLFLAG_PERMANENT,
                       CTLTYPE_STRING, "name", NULL,
-                      NULL, 0, __UNCONST("Example"), 0
+                      NULL, 0, __UNCONST(SECMODEL_EXAMPLE_NAME), 0
                       CTL_CREATE, CTL_EOL);
+}
 
+/*
+ * Initialize the security model.
+ */
+static void
+secmodel_example_init(void)
+{
+
+       /* typically used to set static variables and states */
 }
 
 /*
  * Start the security model.
  */
-void
-secmodel_start(void)
+static void
+secmodel_example_start(void)
 {
-       secmodel_example_init();
+
+       /* register listeners */
+       l_device = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
+           secmodel_example_device_cb, NULL);
+       l_generic = kauth_listen_scope(KAUTH_SCOPE_GENERIC,
+           secmodel_example_generic_cb, NULL);
+       l_machdep = kauth_listen_scope(KAUTH_SCOPE_MACHDEP,
+           secmodel_example_machdep_cb, NULL);
+       l_network = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
+           secmodel_example_network_cb, NULL);
+       l_process = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
+           secmodel_example_process_cb, NULL);
+       l_system = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
+           secmodel_example_system_cb, NULL);
+       l_vnode = kauth_listen_scope(KAUTH_SCOPE_VNODE,
+           secmodel_example_vnode_cb, NULL);
+}
+
+/*
+ * Stop the security model.
+ */
+static void
+secmodel_example_stop(void)
+{
+
+       /* unregister listeners */
+       kauth_unlisten_scope(l_device);
+       kauth_unlisten_scope(l_generic);
+       kauth_unlisten_scope(l_machdep);
+       kauth_unlisten_scope(l_network);
+       kauth_unlisten_scope(l_process);
+       kauth_unlisten_scope(l_system);
+       kauth_unlisten_scope(l_vnode);
+}
+
+/*
+ * An evaluation routine example. That one will allow any secmodel(9)
+ * to request to secmodel_example if "is-example-useful". We consider
+ * that it is, so return yes.
+ */
+static int
+secmodel_example_eval(const char *what, void *arg, void *ret)
+{
+       int error = 0;
 
-       kauth_listen_scope(KAUTH_SCOPE_GENERIC,
-          secmodel_example_generic_cb, NULL);
-       kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
-          secmodel_example_system_cb, NULL);
-       kauth_listen_scope(KAUTH_SCOPE_PROCESS,
-          secmodel_example_process_cb, NULL);
-       kauth_listen_scope(KAUTH_SCOPE_NETWORK,
-          secmodel_example_network_cb, NULL);
-       kauth_listen_scope(KAUTH_SCOPE_MACHDEP,
-          secmodel_example_machdep_cb, NULL);
+       if (strcasecmp(what, "is-example-useful") == 0) {
+               bool *bp = ret;
+               *bp = true;
+       } else {
+               error = ENOENT;
+       }
+
+       return error;
+}
+
+/*
+ * Module attachement/detachement routine. Whether the secmodel(9) is
+ * builtin or loaded dynamically, it is in charge of initializing, starting
+ * and stopping the module. See module(9).
+ */
+
+static int
+secmodel_example_modcmd(modcmd_t cmd, void *arg)
+{
+       int error = 0;
+
+       switch (cmd) {
+       case MODULE_CMD_INIT:
+               secmodel_example_init();
+               secmodel_example_start();
+               sysctl_security_example_setup(&sysctl_example_log);
+
+               error = secmodel_register(&example_sm,
+                   SECMODEL_EXAMPLE_ID, SECMODEL_EXAMPLE_NAME,
+                   NULL, secmodel_example_eval, NULL);
+               if (error != 0)
+                       printf("secmodel_example_modcmd::init: "
+                           "secmodel_register returned %d\n", error);
+
+               break;
+
+       case MODULE_CMD_FINI:
+               error = secmodel_deregister(example_sm);
+               if (error != 0)
+                       printf("secmodel_example_modcmd::fini: "
+                           "secmodel_deregister returned %d\n", error);
+
+               sysctl_teardown(&sysctl_example_log);
+               secmodel_example_stop();
+               break;
+
+       default:
+               error = ENOTTY;
+               break;
+       }
+
+       return error;
 }
 
 /*
  * Security model: example
  * Scope: Generic
  */
-int
-secmodel_example_generic_cb(kauth_cred_t, kauth_action_t action,
+static int
+secmodel_example_generic_cb(kauth_cred_t cred, kauth_action_t action,
     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
 {
        int result;
@@ -113,7 +217,6 @@
 
        switch(action) {
        case KAUTH_GENERIC_ISSUSER:
-       case KAUTH_GENERIC_CANSEE:
        default:
                result = KAUTH_RESULT_DEFER;
                break;
@@ -126,7 +229,7 @@
  * Security model: example
  * Scope: System
  */
-int
+static int
 secmodel_example_system_cb(kauth_cred_t cred, kauth_action_t action,
     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
 {
@@ -153,7 +256,6 @@
        case KAUTH_SYSTEM_TIME:
                switch (req) {
                case KAUTH_REQ_SYSTEM_TIME_ADJTIME:
-               case KAUTH_REQ_SYSTEM_TIME_BACKWARDS:
                case KAUTH_REQ_SYSTEM_TIME_NTPADJTIME:
                case KAUTH_REQ_SYSTEM_TIME_RTCOFFSET:
                case KAUTH_REQ_SYSTEM_TIME_SYSTEM:
@@ -169,6 +271,7 @@
                case KAUTH_REQ_SYSTEM_SYSCTL_ADD:
                case KAUTH_REQ_SYSTEM_SYSCTL_DELETE:
                case KAUTH_REQ_SYSTEM_SYSCTL_DESC:
+               case KAUTH_REQ_SYSTEM_SYSCTL_PRVT:
                default:
                        result = KAUTH_RESULT_DEFER;
                        break;
@@ -215,10 +318,22 @@
                }
                break;
 
-       case KAUTH_SYSTEM_LKM:
+       case KAUTH_SYSTEM_FS_QUOTA:
+               switch (req) {
+               case KAUTH_REQ_SYSTEM_FS_QUOTA_GET:
+               case KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF:
+               case KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE:
+               case KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT:
+               default:
+                       result = KAUTH_RESULT_DEFER;
+                       break;
+               }



Home | Main Index | Thread Index | Old Index