예제 #1
0
            internal Info(Sandbox.ManagedFailureCallback failureCallback, SandboxedProcessUnix process, string reportsFifoPath, string famPath, string debugLogPath)
            {
                m_failureCallback = failureCallback;
                Process           = process;
                ReportsFifoPath   = reportsFifoPath;
                FamPath           = famPath;
                DebugLogJailPath  = debugLogPath;

                m_pathCache       = new Dictionary <string, PathCacheRecord>();
                m_activeProcesses = new ConcurrentDictionary <int, byte>
                {
                    [process.ProcessId] = 1
                };
                m_activeProcessesChecker = new CancellableTimedAction(
                    CheckActiveProcesses,
                    intervalMs: Math.Min((int)process.ChildProcessTimeout.TotalMilliseconds, (int)ActiveProcessesCheckerInterval.TotalMilliseconds));

                // create a write handle (used to keep the fifo open, i.e.,
                // the 'read' syscall won't receive EOF until we close this writer
                m_lazyWriteHandle = new Lazy <SafeFileHandle>(() =>
                {
                    LogDebug($"Opening FIFO '{ReportsFifoPath}' for writing");
                    return(IO.Open(ReportsFifoPath, IO.OpenFlags.O_WRONLY, 0));
                });

                // start a background thread for reading from the FIFO
                m_workerThread = new Thread(StartReceivingAccessReports);
                m_workerThread.IsBackground = true;
                m_workerThread.Priority     = ThreadPriority.Highest;
            }
예제 #2
0
        public void CancellingNotStartedActionTest()
        {
            int counter = 0;

            using (var timedAction = new CancellableTimedAction(() => ++ counter, 10, nameof(CancellingNotStartedActionTest)))
            {
                Thread.Sleep(500);
                timedAction.Cancel();
                timedAction.Join();
                XAssert.AreEqual(0, counter);
            }
        }
        /// <nodoc />
        public SandboxedProcessUnix(SandboxedProcessInfo info, bool ignoreReportedAccesses = false, bool?overrideMeasureTime = null)
            : base(info)
        {
            Contract.Requires(info.FileAccessManifest != null);
            Contract.Requires(info.SandboxConnection != null);

            PipId = info.FileAccessManifest.PipId;

            SandboxConnection   = info.SandboxConnection;
            ChildProcessTimeout = info.NestedProcessTerminationTimeout;
            AllowedSurvivingChildProcessNames = info.AllowedSurvivingChildProcessNames;
            ReportQueueProcessTimeoutForTests = info.ReportQueueProcessTimeoutForTests;
            IgnoreReportedAccesses            = ignoreReportedAccesses;
            RootJailInfo = info.RootJailInfo;

            MeasureCpuTime = overrideMeasureTime.HasValue
                ? overrideMeasureTime.Value
                : info.SandboxConnection.MeasureCpuTimes;

            m_perfAggregator = new PerfAggregator();

            m_perfCollector = new CancellableTimedAction(
                callback: UpdatePerfCounters,
                intervalMs: (int)PerfProbeInternal.TotalMilliseconds);

            m_reports = new SandboxedProcessReports(
                info.FileAccessManifest,
                info.PathTable,
                info.PipSemiStableHash,
                info.PipDescription,
                info.LoggingContext,
                info.DetoursEventListener,
                info.SidebandWriter,
                info.FileSystemView);

            var useSingleProducer = !(SandboxConnection.Kind == SandboxKind.MacOsHybrid || SandboxConnection.Kind == SandboxKind.MacOsDetours);

            var executionOptions = new ExecutionDataflowBlockOptions
            {
                EnsureOrdered             = true,
                SingleProducerConstrained = useSingleProducer,
                BoundedCapacity           = DataflowBlockOptions.Unbounded,
                MaxDegreeOfParallelism    = 1 // Must be one, otherwise SandboxedPipExecutor will fail asserting valid reports
            };

            m_pendingReports = new ActionBlock <AccessReport>(HandleAccessReport, executionOptions);

            // install a 'ProcessStarted' handler that informs the sandbox of the newly started process
            ProcessStarted += (pid) => OnProcessStartedAsync(info).GetAwaiter().GetResult();
        }
예제 #4
0
        public void LongIntervalTest()
        {
            int counter = 0;

            using (var timedAction = new CancellableTimedAction(() => Interlocked.Increment(ref counter), 1000, nameof(LongIntervalTest)))
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                XAssert.IsTrue(timedAction.Start());
                Thread.Sleep(100);
                timedAction.Cancel();
                timedAction.Join();
                sw.Stop();
                XAssert.AreEqual(1, counter);
                XAssert.IsTrue(sw.ElapsedMilliseconds < 1000);
            }
        }
            internal Info(Sandbox.ManagedFailureCallback failureCallback, SandboxedProcessUnix process, string reportsFifoPath, string famPath, string debugLogPath, bool isInTestMode)
            {
                m_isInTestMode       = isInTestMode;
                m_stopRequestCounter = 0;
                m_completeAccessReportProcessingCounter = 0;
                m_failureCallback = failureCallback;
                Process           = process;
                ReportsFifoPath   = reportsFifoPath;
                FamPath           = famPath;
                DebugLogJailPath  = debugLogPath;

                m_waitToCompleteCts = new CancellationTokenSource();
                m_pathCache         = new Dictionary <string, PathCacheRecord>();
                m_activeProcesses   = new ConcurrentDictionary <int, byte>
                {
                    [process.ProcessId] = 1
                };
                m_activeProcessesChecker = new CancellableTimedAction(
                    CheckActiveProcesses,
                    intervalMs: Math.Min((int)process.ChildProcessTimeout.TotalMilliseconds, (int)ActiveProcessesCheckerInterval.TotalMilliseconds));

                // create a write handle (used to keep the fifo open, i.e.,
                // the 'read' syscall won't receive EOF until we close this writer
                m_lazyWriteHandle = new Lazy <SafeFileHandle>(() =>
                {
                    LogDebug($"Opening FIFO '{ReportsFifoPath}' for writing");
                    return(IO.Open(ReportsFifoPath, IO.OpenFlags.O_WRONLY, 0));
                });

                // action block where parsing and processing of received ActionReport bytes is done
                m_accessReportProcessingBlock = new ActionBlock <(PooledObjectWrapper <byte[]> wrapper, int length)>(ProcessBytes, new ExecutionDataflowBlockOptions
                {
                    BoundedCapacity        = DataflowBlockOptions.Unbounded,
                    MaxDegreeOfParallelism = 1,
                    EnsureOrdered          = true
                });

                // start a background thread for reading from the FIFO
                m_workerThread = new Thread(StartReceivingAccessReports);
                m_workerThread.IsBackground = true;
                m_workerThread.Priority     = ThreadPriority.Highest;
            }
예제 #6
0
        public void MultipleActionStartsTest()
        {
            var mre     = new ManualResetEvent(false);
            int counter = 0;

            using (var timedAction = new CancellableTimedAction(() =>
            {
                // Signal that thread is started
                mre.Set();
                ++counter;
            }, 10, nameof(MultipleActionStartsTest)))
            {
                XAssert.IsTrue(timedAction.Start());
                XAssert.IsFalse(timedAction.Start());
                Thread.Sleep(500);
                XAssert.IsFalse(timedAction.Start());
                timedAction.Cancel();
                timedAction.Join();
                XAssert.IsTrue(counter > 2, "Value of counter is " + counter);
            }
        }
예제 #7
0
        /// <nodoc />
        public SandboxedProcessMac(SandboxedProcessInfo info, bool ignoreReportedAccesses = false, bool?overrideMeasureTime = null)
            : base(info)
        {
            Contract.Requires(info.FileAccessManifest != null);
            Contract.Requires(info.SandboxConnection != null);

            IgnoreReportedAccesses = ignoreReportedAccesses;

            MeasureCpuTime = overrideMeasureTime.HasValue
                ? overrideMeasureTime.Value
                : info.SandboxConnection.MeasureCpuTimes;

            m_perfAggregator = new PerfAggregator();

            m_perfCollector = new CancellableTimedAction(
                callback: UpdatePerfCounters,
                intervalMs: (int)PerfProbeInternal.TotalMilliseconds);

            m_reports = new SandboxedProcessReports(
                info.FileAccessManifest,
                info.PathTable,
                info.PipSemiStableHash,
                info.PipDescription,
                info.LoggingContext,
                info.DetoursEventListener,
                info.SharedOpaqueOutputLogger);

            m_pendingReports = new ActionBlock <AccessReport>(
                HandleAccessReport,
                new ExecutionDataflowBlockOptions
            {
                EnsureOrdered          = true,
                BoundedCapacity        = DataflowBlockOptions.Unbounded,
                MaxDegreeOfParallelism = 1,     // Must be one, otherwise SandboxedPipExecutor will fail asserting valid reports
            });

            // install a 'ProcessStarted' handler that informs the sandbox of the newly started process
            ProcessStarted += () => OnProcessStartedAsync().GetAwaiter().GetResult();
        }
예제 #8
0
        public void BasicFunctionalityTest()
        {
            var mre     = new ManualResetEvent(false);
            int counter = 0;

            using (var timedAction = new CancellableTimedAction(() =>
            {
                // Signal that thread is started
                mre.Set();
                ++counter;
            }, 10, nameof(BasicFunctionalityTest)))
            {
                XAssert.IsTrue(timedAction.Start());

                // Wait for thread to start
                mre.WaitOne();

                Thread.Sleep(500);
                timedAction.Cancel();
                timedAction.Join();
                XAssert.IsTrue(counter > 2, "Value of counter is " + counter);
            }
        }