+Workaround Linux kernel last device block inaccessibility.
[captive.git] / src / libcaptive / sandbox / split.c
index 1556462..d58c8ee 100644 (file)
@@ -28,7 +28,6 @@
 #include "captive/rtl-file.h"
 #include <unistd.h>
 #include <fcntl.h>
-#include <linc/linc.h> /* for linc_main_get_loop() */
 #include "server-Directory.h"
 #include "server-Vfs.h"
 #include "../client/vfs.h"
 #include <errno.h>
 #include "../client/giochannel-blind.h"        /* for captive_giochannel_blind_new() */
 #include <signal.h>
+#include "../storage/relastblock.h"    /* for captive_storage_relastblock() */
+
+#ifdef HAVE_ORBIT_LINK
+void link_set_tmpdir(const char *dir);
+#else
+#include <linc/linc.h> /* for linc_main_get_loop() */
+#endif
 
 
 /* CONFIG: */
@@ -76,13 +82,19 @@ gchar *orb_argv[]={
        g_return_val_if_fail(poap!=NULL,FALSE);
 
        if (done)
-               return TRUE;
+               return TRUE;    /* FIXME: '*poap' is left invalid! */
 
        /* Init 'ev' */
        CORBA_exception_init(evp);
 
        /* Init 'orb' */
-       *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-orb",evp);
+       /* libcaptive is single-threaded only, caller must lock it.
+        * If thread A spawned the sandbox while currently doing its own work
+        * and thread B calls the sandbox thread B waits on ORB_run()
+        * while the sandbox waits for the response of thread A ORB. Deadlock.
+        * "orbit-local-non-threaded-orb" requests thread unaware ORB.
+        */
+       *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-non-threaded-orb",evp);
        g_return_val_if_fail(*orbp!=CORBA_OBJECT_NIL,FALSE);
        g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
 
@@ -168,13 +180,30 @@ void sandbox_child_prepare_shutdown(void)
 }
 #endif
 
+GMainLoop *captive_corba_get_loop(void)
+{
+#ifdef HAVE_ORBIT_LINK
+static GMainLoop *link_loop;
+
+       if (!link_loop)
+               link_loop=g_main_loop_new(g_main_context_default(),TRUE);
+       return link_loop;
+#else
+       return linc_main_get_loop();
+#endif
+}
+
+GMainContext *captive_corba_get_context(void)
+{
+       return g_main_loop_get_context(captive_corba_get_loop());
+}
 
 void sandbox_child_shutdown(void)
 {
        /* Do not fail by passing logging messages to the master. */
        impl_Captive_Vfs_init_g_log_func_disable();
 
-       g_main_loop_quit(linc_main_get_loop());
+       g_main_loop_quit(captive_corba_get_loop());
 }
 
 
@@ -243,10 +272,10 @@ int errint;
 
        heartbeat_source_callback_orb=captive_corba_orb;
 
-       /* linc_main_get_loop() makes sense only after corba_init() -> CORBA_ORB_init() */
+       /* captive_corba_get_context() makes sense only after corba_init() -> CORBA_ORB_init() */
        errguint=g_source_attach(
                        captive_corba_sandbox_child_heartbeat_gsource,  /* source */
-                       g_main_loop_get_context(linc_main_get_loop())); /* context; NULL means 'default context' */
+                       captive_corba_get_context());   /* context; NULL means 'default context' */
        g_assert(errguint!=0);
 
        /* Init 'Vfs_object' */
@@ -305,8 +334,12 @@ const gchar *socketname=NULL,*socketpathname;
        /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
         * and therefore we should be safe with glib events handling.
         */
+#ifdef HAVE_ORBIT_LINK
+       g_main_loop_run(captive_corba_get_loop());
+#else
        CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
+#endif
 
        /* Shutdown 'Vfs' servant */
        Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
@@ -331,11 +364,12 @@ static void options_module_captive_to_options_module_corba
 
        g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
 
-       dest_options_module_corba->pathname_utf8=g_strdup(src_options_module_captive->pathname_utf8);
-       dest_options_module_corba->data._buffer=g_memdup(src_options_module_captive->u.pe32.base,
-                       src_options_module_captive->u.pe32.length);
+       dest_options_module_corba->pathname_utf8=CORBA_string_dup(src_options_module_captive->pathname_utf8);
        dest_options_module_corba->data._maximum=src_options_module_captive->u.pe32.length;
        dest_options_module_corba->data._length =src_options_module_captive->u.pe32.length;
+       dest_options_module_corba->data._buffer=Captive_Bytes_allocbuf(dest_options_module_corba->data._maximum);
+       memcpy(dest_options_module_corba->data._buffer,src_options_module_captive->u.pe32.base,
+                       src_options_module_captive->u.pe32.length);
        dest_options_module_corba->data._release=TRUE;
 }
 
@@ -373,6 +407,7 @@ static void sandbox_parent_own_orbit_dir_cleanup_atexit(void)
 static gboolean done=FALSE;
 
        g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s; done=%d",G_STRLOC,(int)done);
+
        if (done)
                return;
        done=TRUE;
@@ -408,11 +443,15 @@ struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
        signal(signum,sigstructp->sighandler_orig);
 
        /* Prevent recursive fatal logging before signal restore: */
-       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d,sighandler_orig=%p",G_STRLOC,signum,sigstructp->sighandler_orig);
+       /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d,sighandler_orig=%p",G_STRLOC,signum,sigstructp->sighandler_orig);
+        * as it is dangerous to g_log() from sighandler.
+        */
 
        sandbox_parent_own_orbit_dir_cleanup_atexit();
 
-       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: re-raising...",G_STRLOC);
+       /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: re-raising...",G_STRLOC);
+        * as it is dangerous to g_log() from sighandler.
+        */
        raise(signum);
 }
 
@@ -430,7 +469,8 @@ struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
                }
 }
 
-gchar *sandbox_parent_read_ior(int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp)
+static gchar *sandbox_parent_read_ior
+               (int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp,CaptiveVfsParentObject *captive_vfs_parent_object)
 {
 gchar *data;
 gsize data_size;
@@ -446,7 +486,8 @@ gboolean errbool;
 
        /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
        if (!sandbox_parent_own_orbit_dir) {
-               sandbox_parent_own_orbit_dir=g_strdup_printf("/tmp/captive-orbit-%d",getpid());
+               /* FIXME: Make 'CAPTIVE_SANDBOX_CHROOT' configurable. */
+               sandbox_parent_own_orbit_dir=g_strdup_printf("%s/tmp/captive-orbit-%d",CAPTIVE_SANDBOX_CHROOT,getpid());
                if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
                        g_assert(errno==EEXIST);
                        sandbox_parent_own_orbit_dir_cleanup_init();
@@ -455,7 +496,11 @@ gboolean errbool;
                                g_assert_not_reached();
                        }
                sandbox_parent_own_orbit_dir_cleanup_init();
+#ifdef HAVE_ORBIT_LINK
+               link_set_tmpdir(sandbox_parent_own_orbit_dir);
+#else
                linc_set_tmpdir(sandbox_parent_own_orbit_dir);
+#endif
                }
 
        data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
@@ -485,17 +530,23 @@ const gchar *chrooted_orbit_dir;
                /* IOR contains the full pathname with the setuid username encoded. */
                chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
                g_assert(chrooted_orbit_dir!=NULL);
-               if (mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
+               captive_vfs_parent_object->corba_chrooted_orbit_dir=g_strdup(chrooted_orbit_dir);
+               /* 0700 as this directory will not be reused
+                * to commuicate with any other sandbox child.
+                */
+               if (mkdir(chrooted_orbit_dir,0700)) {
                        g_assert(errno==EEXIST);
                        }
 
                socketname=g_hash_table_lookup(hash,"socketname");
                g_assert(socketname!=NULL);
+               captive_vfs_parent_object->corba_socketname=g_strdup(socketname);
 
                socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
-               socketpathname_dest=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
+               socketpathname_dest=g_strdup_printf("%s/%s",chrooted_orbit_dir,socketname);
                errint=link(socketpathname_src,socketpathname_dest);
-               g_assert(errint==0);
+               if (errint)
+                       g_error("link(\"%s\",\"%s\")=%d:%m",socketpathname_src,socketpathname_dest,errint);
                }
        if (child_chroot_pid_hashkey_dirp)
                *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
@@ -518,7 +569,8 @@ static xmlNode *options_module_captive_to_xml
        g_return_val_if_fail(src_options_module_captive!=NULL,NULL);
 
        { xmlNode *module=xmlNewTextChild(dest_xml_parent,NULL,"module",NULL);
-       const gchar *type_string,*basename,*cgs;
+       const gchar *type_string="???"; /* Prevent: ... might be used uninitialized in this function */
+       const gchar *basename,*cgs;
 
                basename=src_options_module_captive->pathname_utf8;
                if ((cgs=strrchr(basename,'/')))
@@ -601,19 +653,32 @@ int errint;
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
 
        /* Init 'CaptiveIOChannel_object' */
-       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source)
+       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source) {
+GIOChannel *giochannel;
+
+               giochannel=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
+               g_io_channel_ref(giochannel);
+
+               giochannel=captive_storage_relastblock(giochannel);
+
                switch (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode) {
                        case CAPTIVE_OPTION_RWMODE_RO:
                        case CAPTIVE_OPTION_RWMODE_RW:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
                                break;
-                       case CAPTIVE_OPTION_RWMODE_BLIND:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
-                                               CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel, /* giochannel_orig */
+                       case CAPTIVE_OPTION_RWMODE_BLIND: {
+GIOChannel *giochannel_orig;
+
+                               giochannel_orig=giochannel;
+                               giochannel=(GIOChannel *)captive_giochannel_blind_new(
+                                               giochannel,     /* giochannel_orig */
                                                TRUE); /* writeable */
-                               break;
+                               g_io_channel_unref(giochannel_orig);    /* reffed by captive_giochannel_blind_new() */
+                               } break;
                        default: g_assert_not_reached();
                        }
+               captive_vfs_parent_object->corba_parent_giochanel_blind_source=giochannel;
+               }
+
        if (!captive_vfs_parent_object->corba_parent_giochanel_blind)
                captive_vfs_parent_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
                                captive_vfs_parent_object->corba_parent_giochanel_blind_source,  /* giochannel_orig */
@@ -653,9 +718,29 @@ struct dirent *dirent;
                }
 
        if (child_chroot_pid_hashkey_dir) {
+gchar *s;
+
                child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
-               errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
-               g_assert(errint==0);
+               s=(/* de-const */ gchar *)child_chroot_parent_own_orbit_dir;
+               do {
+                       s=strchr(s,'/');
+                       if (s)
+                               *s=0;
+                       if (*child_chroot_parent_own_orbit_dir) {
+                               errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
+                               if (errint)
+                                       g_assert(errno==EEXIST);
+                               else {
+                                       /* chmod(2) it to prevent mode limitation by
+                                        * active ulimit(2) of being executed by mount(8).
+                                        */
+                                       errint=chmod(child_chroot_parent_own_orbit_dir,0777);
+                                       g_assert(errint==0);
+                                       }
+                               }
+                       if (s)
+                               *s++='/';
+                       } while (s);
                child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
                                child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
                errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
@@ -665,7 +750,9 @@ struct dirent *dirent;
                g_assert(errint==0);
                }
 
-       options_corba.g_log_func=GLogFunc_object;
+       options_corba.g_log_func=CORBA_Object_duplicate(GLogFunc_object,&captive_corba_ev);
+       g_assert(validate_CORBA_Environment(&captive_corba_ev));
+
        options_module_captive_to_options_module_corba(&options_corba.filesystem,&CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.filesystem);
        /* Prevent secondary captive_giochannel_blind inside of our sandbox child
         * as we already have one captive_giochannel_blind in the parent.
@@ -674,12 +761,14 @@ struct dirent *dirent;
                        : CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode);
        options_corba.media         =CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.media;
        options_corba.debug_messages=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.debug_messages;
-       options_corba.image_iochannel=CaptiveIOChannel_object;
+
+       options_corba.image_iochannel=CORBA_Object_duplicate(CaptiveIOChannel_object,&captive_corba_ev);
+       g_assert(validate_CORBA_Environment(&captive_corba_ev));
 
        load_module_length=g_list_length(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.load_module);
-       captive_newn(options_corba.load_module._buffer,load_module_length);
        options_corba.load_module._maximum=load_module_length;
        options_corba.load_module._length=load_module_length;
+       options_corba.load_module._buffer=Captive_CaptiveOptionsModuleList_allocbuf(options_corba.load_module._maximum);
        options_corba.load_module._release=TRUE;
        for (load_moduleui=0,load_module_node=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.load_module;
                        load_module_node;
@@ -691,7 +780,8 @@ struct dirent *dirent;
        Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
 
-       /* FIXME: Free 'options_corba' - LEAK */
+       Captive_CaptiveOptions__freekids(&options_corba,
+                       NULL);  /* d; unused */
 
        captive_vfs_parent_object->corba_Vfs_object=Vfs_object;
        captive_vfs_parent_object->corba_GLogFunc_object=GLogFunc_object;
@@ -831,7 +921,8 @@ gchar *child_chroot_pid_hashkey_dir;
 
                        Vfs_IOR=sandbox_parent_read_ior(
                                        Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
-                                       &child_chroot_pid_hashkey_dir);
+                                       &child_chroot_pid_hashkey_dir,
+                                       captive_vfs_parent_object);
 
                        sandbox_parent(
                                        Vfs_IOR,        /* Vfs_IOR */