7199N/AFrom 1564b58074df543f4e90b2fee79199c5dd36afca Mon Sep 17 00:00:00 2001
7199N/AFrom: Alan Coopersmith <alan.coopersmith@oracle.com>
7199N/ADate: Tue, 29 Dec 2015 13:35:20 -0800
7199N/ASubject: [PATCH 05/19] smf contracts
7199N/A
7199N/ABug 15491359 - SUNBT6720967 gdm is insufficiently contract-aware
7199N/AOriginal date:2008-05-05 owner:yippi type:feature doo:14007
7199N/A---
7199N/A acconfig.h | 1 +
7199N/A configure.ac | 8 +++
7199N/A daemon/gdm-session-worker-job.c | 151 ++++++++++++++++++++++++++++++++++++++++
7199N/A 3 files changed, 160 insertions(+)
7199N/A
7199N/Adiff --git a/acconfig.h b/acconfig.h
7199N/Aindex c8a1d96..a6bd4be 100644
7199N/A--- a/acconfig.h
7199N/A+++ b/acconfig.h
7199N/A@@ -28,6 +28,7 @@
7199N/A #undef HAVE_SETENV
7199N/A #undef HAVE_SETRESUID
7199N/A #undef HAVE_SHADOW
7199N/A+#undef HAVE_SMF_CONTRACTS
7199N/A #undef HAVE_SOLARIS_XINERAMA
7199N/A #undef HAVE_STPCPY
7199N/A #undef HAVE_SYS_SOCKIO_H
7199N/Adiff --git a/configure.ac b/configure.ac
7199N/Aindex 0ada667..5e97a41 100644
7199N/A--- a/configure.ac
7199N/A+++ b/configure.ac
7199N/A@@ -965,6 +965,14 @@ if test "x$enable_rbac_shutdown" != "xno"; then
7199N/A fi
7199N/A AC_SUBST(RBAC_LIBS)
7199N/A
7199N/A+dnl ---------------------------------------------------------------------------
7199N/A+dnl check for Solaris SMF contract support
7199N/A+dnl ---------------------------------------------------------------------------
7199N/A+
7199N/A+AC_MSG_CHECKING(for Solaris SMF contract support)
7199N/A+AC_CHECK_LIB(contract, ct_tmpl_activate, [
7199N/A+ AC_DEFINE(HAVE_SMF_CONTRACTS)
7199N/A+ EXTRA_DAEMON_LIBS="$EXTRA_DAEMON_LIBS -lcontract" ])
7199N/A
7199N/A dnl ---------------------------------------------------------------------------
7199N/A dnl check for backtrace support
7199N/Adiff --git a/daemon/gdm-session-worker-job.c b/daemon/gdm-session-worker-job.c
7199N/Aindex 52a6e9f..a1363e9 100644
7199N/A--- a/daemon/gdm-session-worker-job.c
7199N/A+++ b/daemon/gdm-session-worker-job.c
7199N/A@@ -45,6 +45,13 @@
7199N/A #include <systemd/sd-journal.h>
7199N/A #endif
7199N/A
7199N/A+#ifdef HAVE_SMF_CONTRACTS
7199N/A+#include <sys/ctfs.h>
7199N/A+#include <sys/contract.h>
7199N/A+#include <sys/contract/process.h>
7199N/A+#include <libcontract.h>
7199N/A+#endif
7199N/A+
7199N/A #include <glib.h>
7199N/A #include <glib/gi18n.h>
7199N/A #include <glib-object.h>
7199N/A@@ -117,9 +124,145 @@ session_worker_job_setup_journal_fds (void)
7199N/A return;
7199N/A }
7199N/A
7199N/A+#ifdef HAVE_SMF_CONTRACTS
7199N/A+static int contracts_fd = -1;
7199N/A+
7199N/A+static void
7199N/A+contracts_pre_fork (void)
7199N/A+{
7199N/A+ const char *errmsg = "opening process contract template";
7199N/A+
7199N/A+ /*
7199N/A+ * On failure, just continue since it is better to start with
7199N/A+ * children in the same contract than to not start them at all.
7199N/A+ */
7199N/A+ if (contracts_fd == -1) {
7199N/A+ if ((contracts_fd = open64 (CTFS_ROOT "/process/template",
7199N/A+ O_RDWR)) == -1)
7199N/A+ goto exit;
7199N/A+
7199N/A+ errmsg = "setting contract terms";
7199N/A+ if ((errno = ct_pr_tmpl_set_param (contracts_fd, CT_PR_PGRPONLY)))
7199N/A+ goto exit;
7199N/A+
7199N/A+ if ((errno = ct_tmpl_set_informative (contracts_fd, CT_PR_EV_HWERR)))
7199N/A+ goto exit;
7199N/A+
7199N/A+ if ((errno = ct_pr_tmpl_set_fatal (contracts_fd, CT_PR_EV_HWERR)))
7199N/A+ goto exit;
7199N/A+
7199N/A+ if ((errno = ct_tmpl_set_critical (contracts_fd, 0)))
7199N/A+ goto exit;
7199N/A+ }
7199N/A+
7199N/A+ errmsg = "setting active template";
7199N/A+ if ((errno = ct_tmpl_activate (contracts_fd)))
7199N/A+ goto exit;
7199N/A+
7199N/A+ g_debug ("Set active contract");
7199N/A+ return;
7199N/A+
7199N/A+exit:
7199N/A+ if (contracts_fd != -1)
7199N/A+ (void) close (contracts_fd);
7199N/A+
7199N/A+ contracts_fd = -1;
7199N/A+
7199N/A+ if (errno) {
7199N/A+ g_debug ("Error setting up active contract template: %s while %s",
7199N/A+ strerror (errno), errmsg);
7199N/A+ }
7199N/A+}
7199N/A+
7199N/A+static void
7199N/A+contracts_post_fork_child (void)
7199N/A+{
7199N/A+ /* Clear active template so no new contracts are created on fork */
7199N/A+ if (contracts_fd == -1)
7199N/A+ return;
7199N/A+
7199N/A+ if ((errno = (ct_tmpl_clear (contracts_fd)))) {
7199N/A+ g_debug (
7199N/A+ "Error clearing active contract template (child): %s",
7199N/A+ strerror (errno));
7199N/A+ } else {
7199N/A+ g_debug ("Cleared active contract template (child)");
7199N/A+ }
7199N/A+
7199N/A+ (void) close (contracts_fd);
7199N/A+
7199N/A+ contracts_fd = -1;
7199N/A+}
7199N/A+
7199N/A+static void
7199N/A+contracts_post_fork_parent (int fork_succeeded)
7199N/A+{
7199N/A+ char path[PATH_MAX];
7199N/A+ int cfd;
7199N/A+ ct_stathdl_t status;
7199N/A+ ctid_t latest;
7199N/A+
7199N/A+ /* Clear active template, abandon latest contract. */
7199N/A+ if (contracts_fd == -1)
7199N/A+ return;
7199N/A+
7199N/A+ if ((errno = ct_tmpl_clear (contracts_fd)))
7199N/A+ g_debug ("Error while clearing active contract template: %s",
7199N/A+ strerror (errno));
7199N/A+ else
7199N/A+ g_debug ("Cleared active contract template (parent)");
7199N/A+
7199N/A+ if (!fork_succeeded)
7199N/A+ return;
7199N/A+
7199N/A+ if ((cfd = open64 (CTFS_ROOT "/process/latest", O_RDONLY)) == -1) {
7199N/A+ g_debug ("Error getting latest contract: %s",
7199N/A+ strerror(errno));
7199N/A+ return;
7199N/A+ }
7199N/A+
7199N/A+ if ((errno = ct_status_read (cfd, CTD_COMMON, &status)) != 0) {
7199N/A+ g_debug ("Error getting latest contract ID: %s",
7199N/A+ strerror(errno));
7199N/A+ (void) close (cfd);
7199N/A+ return;
7199N/A+ }
7199N/A+
7199N/A+ latest = ct_status_get_id (status);
7199N/A+ ct_status_free (status);
7199N/A+ (void) close (cfd);
7199N/A+
7199N/A+ if ((snprintf (path, PATH_MAX, CTFS_ROOT "/all/%ld/ctl",
7199N/A+ (long) latest)) >= PATH_MAX) {
7199N/A+ g_debug ("Error opening the latest contract ctl file: %s",
7199N/A+ strerror (ENAMETOOLONG));
7199N/A+ return;
7199N/A+ }
7199N/A+
7199N/A+ cfd = open64 (path, O_WRONLY);
7199N/A+ if (cfd == -1) {
7199N/A+ g_debug ("Error opening the latest contract ctl file: %s",
7199N/A+ strerror (errno));
7199N/A+ return;
7199N/A+ }
7199N/A+
7199N/A+ if ((errno = ct_ctl_abandon (cfd)))
7199N/A+ g_debug ("Error abandoning latest contract: %s",
7199N/A+ strerror (errno));
7199N/A+ else
7199N/A+ g_debug ("Abandoned latest contract");
7199N/A+
7199N/A+ (void) close (cfd);
7199N/A+}
7199N/A+#endif /* HAVE_SMF_CONTRACTS */
7199N/A+
7199N/A static void
7199N/A session_worker_job_child_setup (GdmSessionWorkerJob *session_worker_job)
7199N/A {
7199N/A+#ifdef HAVE_SMF_CONTRACTS
7199N/A+ contracts_post_fork_child ();
7199N/A+#endif
7199N/A+
7199N/A session_worker_job_setup_journal_fds ();
7199N/A
7199N/A /* Terminate the process when the parent dies */
7199N/A@@ -273,6 +416,10 @@ gdm_session_worker_job_spawn (GdmSessionWorkerJob *session_worker_job,
7199N/A }
7199N/A env = get_job_environment (session_worker_job);
7199N/A
7199N/A+#ifdef HAVE_SMF_CONTRACTS
7199N/A+ contracts_pre_fork ();
7199N/A+#endif
7199N/A+
7199N/A error = NULL;
7199N/A ret = g_spawn_async_with_pipes (NULL,
7199N/A (char **) args->pdata,
7199N/A@@ -286,6 +433,10 @@ gdm_session_worker_job_spawn (GdmSessionWorkerJob *session_worker_job,
7199N/A NULL,
7199N/A &error);
7199N/A
7199N/A+#ifdef HAVE_SMF_CONTRACTS
7199N/A+ contracts_post_fork_parent ((session_worker_job->priv->pid > 0));
7199N/A+#endif
7199N/A+
7199N/A g_ptr_array_foreach (args, (GFunc)g_free, NULL);
7199N/A g_ptr_array_free (args, TRUE);
7199N/A
7199N/A--
7199N/A2.7.4
7199N/A