Mercurial > illumos > illumos-gate
changeset 12552:cffa6779b402
6896885 fmd fabric-xlate doesn't create temporary files securely
author | Scott M. Carter <Scott.Carter@Oracle.COM> |
---|---|
date | Fri, 04 Jun 2010 06:30:13 -0700 |
parents | b2ba48b14377 |
children | e64e5d843075 |
files | usr/src/cmd/fm/modules/common/fabric-xlate/fabric-xlate.c |
diffstat | 1 files changed, 51 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/usr/src/cmd/fm/modules/common/fabric-xlate/fabric-xlate.c Fri Jun 04 11:47:29 2010 +0200 +++ b/usr/src/cmd/fm/modules/common/fabric-xlate/fabric-xlate.c Fri Jun 04 06:30:13 2010 -0700 @@ -20,17 +20,22 @@ */ /* - * Copyright 2010 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. */ #include <fm/libtopo.h> #include <sys/fm/util.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <pthread.h> #include <libxml/xpathInternals.h> #include "fabric-xlate.h" -#define XMLTOPOFILE "/tmp/fab-xlate-topo.xml" +#define XMLTOPOFILE "/var/run/fab-xlate-topo.xml" fmd_xprt_t *fab_fmd_xprt; /* FMD transport layer handle */ char fab_buf[FM_MAX_CLASS]; @@ -39,42 +44,67 @@ static xmlDocPtr fab_doc = NULL; xmlXPathContextPtr fab_xpathCtx = NULL; static int fab_valid_topo = 0; +static pthread_mutex_t fab_lock = PTHREAD_MUTEX_INITIALIZER; static void fab_update_topo(fmd_hdl_t *hdl) { topo_hdl_t *thp = NULL; - FILE *fp; + FILE *fp = NULL; int err = 0; + int fd = -1; - if ((thp = fmd_hdl_topo_hold(hdl, TOPO_VERSION)) == NULL) { - fmd_hdl_debug(hdl, "Failed to hold topo\n"); + /* Open the temporary file with proper ownership */ + while (fd == -1) { + if ((unlink(XMLTOPOFILE) == -1) && (errno != ENOENT)) { + fmd_hdl_debug(hdl, "Failed to remove XML topo file\n"); + return; + } + fd = open(XMLTOPOFILE, O_RDWR | O_CREAT | O_EXCL, 0600); + if ((fd == -1) && (errno != EEXIST)) { + fmd_hdl_debug(hdl, "Failed to create XML topo file\n"); + return; + } } - fp = fopen(XMLTOPOFILE, "w"); + /* Associate a stream with the temporary file */ + if ((fp = fdopen(fd, "w")) == NULL) { + fmd_hdl_debug(hdl, "Failed to open XML topo file\n"); + goto cleanup; + } + /* Hold topology */ + if ((thp = fmd_hdl_topo_hold(hdl, TOPO_VERSION)) == NULL) { + fmd_hdl_debug(hdl, "Failed to hold topo\n"); + goto cleanup; + } + + /* Print topology to XML file */ if (topo_xml_print(thp, fp, FM_FMRI_SCHEME_HC, &err) < 0) { fmd_hdl_debug(hdl, "Failed to get XML topo\n"); + fmd_hdl_topo_rele(hdl, thp); + goto cleanup; } - (void) fclose(fp); - + /* Release topology */ fmd_hdl_topo_rele(hdl, thp); + /* Reload topology from XML file */ if (fab_xpathCtx) xmlXPathFreeContext(fab_xpathCtx); if (fab_doc) xmlFreeDoc(fab_doc); - - /* Load xml document */ fab_doc = xmlParseFile(XMLTOPOFILE); + fab_xpathCtx = xmlXPathNewContext(fab_doc); + fab_set_fake_rp(hdl); + fab_valid_topo = 1; - /* Init xpath */ - fab_xpathCtx = xmlXPathNewContext(fab_doc); - - fab_set_fake_rp(hdl); - - fab_valid_topo = 1; +cleanup: + if (fp != NULL) + (void) fclose(fp); + else if (fd != -1) + (void) close(fd); + (void) unlink(XMLTOPOFILE); } /*ARGSUSED*/ @@ -83,8 +113,10 @@ { nvlist_t *new_nvl; + (void) pthread_mutex_lock(&fab_lock); if (!fab_valid_topo) fab_update_topo(hdl); + (void) pthread_mutex_unlock(&fab_lock); if (nvlist_dup(nvl, &new_nvl, NV_UNIQUE_NAME) != 0) { fmd_hdl_error(hdl, "failed to duplicate event"); @@ -110,7 +142,9 @@ static void fab_topo(fmd_hdl_t *hdl, topo_hdl_t *topo) { + (void) pthread_mutex_lock(&fab_lock); fab_valid_topo = 0; + (void) pthread_mutex_unlock(&fab_lock); } static const fmd_hdl_ops_t fmd_ops = {