Add Simple test program for Tizen nsjail
authorKunhoon Baik <knhoon.baik@samsung.com>
Thu, 15 Jul 2021 09:19:35 +0000 (18:19 +0900)
committerKunhoon Baik <knhoon.baik@samsung.com>
Thu, 15 Jul 2021 09:19:35 +0000 (18:19 +0900)
 - memory limit test
 - syscall(getuid) violation test

The more test programs should be created

packaging/nsjail.spec
test/Makefile [new file with mode: 0644]
test/memory_syscall_test.cc [new file with mode: 0644]

index 157df4be61adef355bbe3857997f79075a7941f7..8b84cb3f63b09980246bd31d8b3b07978578bbea 100644 (file)
@@ -26,6 +26,14 @@ BuildRequires:  protobuf-devel
 A light-weight process isolation tool, making use of Linux namespaces and
 seccomp-bpf syscall filters (with help of the kafel bpf language)
 
+%package test
+Summary:  Tizen nsjail test programs
+Group:    System/Other
+Requires: %{name} = %{version}-%{release}
+
+%description test
+Tizen simple test programs to check nsjail execution validation in Tizen platform
+
 %prep
 %setup -q
 
@@ -34,14 +42,24 @@ export CFLAGS="$CFLAGS -DTIZEN"
 export CXXFLAGS="$CXXFLAGS -DTIZEN"
 make %{?_smp_mflags}
 
+pushd test
+make %{?_smp_mflags}
+popd test
+
 %install
 mkdir -p %{buildroot}/%{_bindir}/
+mkdir -p %{buildroot}/%{_bindir}/nsjail_test/
+
 cp nsjail %{buildroot}/%{_bindir}/
+cp -a test/*_test %{buildroot}/%{_bindir}/nsjail_test/
 
 %files
 %license LICENSE
 %{_bindir}/nsjail
 
+%files test
+%{_bindir}/nsjail_test/*
+
 %changelog
 * Mon Jul 7 2021 Baik 
  - Initial import of version latest version of nsjail - 2021-07-07 version 
diff --git a/test/Makefile b/test/Makefile
new file mode 100644 (file)
index 0000000..40ee221
--- /dev/null
@@ -0,0 +1,68 @@
+#
+#   nsjail - Makefile
+#   -----------------------------------------
+#
+#   Copyright 2014 Google Inc. All Rights Reserved.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+PKG_CONFIG=$(shell command -v pkg-config 2> /dev/null)
+ifeq ($(PKG_CONFIG),)
+$(error "Install pkg-config to make it work")
+endif
+
+CC ?= gcc
+CXX ?= g++
+
+COMMON_FLAGS += -O2 -c \
+       -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 \
+       -fPIE \
+       -Wformat -Wformat-security -Wno-format-nonliteral \
+       -Wall -Wextra -Werror 
+
+CXXFLAGS += $(USER_DEFINES) $(COMMON_FLAGS) \
+       -std=c++11 -fno-exceptions -Wno-unused -Wno-unused-parameter
+LDFLAGS += -pie -Wl,-z,noexecstack -lpthread 
+
+BIN1 = jail_mem_syscall_test
+SRCS1_CXX = memory_syscall_test.cc
+OBJS1 = $(SRCS1_CXX:.cc=.o) 
+
+ifdef DEBUG
+       CXXFLAGS += -g -ggdb -gdwarf-4
+endif
+
+.PHONY: all clean indent
+
+.cc.o: %.cc
+       $(CXX) $(CXXFLAGS) $< -o $@
+
+all: $(BIN1)
+
+$(BIN1): $(OBJS1)
+       $(CXX) -o $(BIN1) $(OBJS1) $(LDFLAGS)
+
+
+.PHONY: clean
+clean:
+       $(RM) core Makefile.bak $(OBJS1) $(BIN1)
+
+
+.PHONY: indent
+indent:
+       clang-format -style="{BasedOnStyle: google, IndentWidth: 8, UseTab: Always, IndentCaseLabels: false, ColumnLimit: 100, AlignAfterOpenBracket: false, AllowShortFunctionsOnASingleLine: false}" -i -sort-includes *.h $(SRCS_CXX)
+
+# DO NOT DELETE THIS LINE -- make depend depends on it.
+
+jail_mem_syscall_test.o: 
+
diff --git a/test/memory_syscall_test.cc b/test/memory_syscall_test.cc
new file mode 100644 (file)
index 0000000..91e2a21
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main()
+{
+       char *a;
+       int i;
+       int k=0;
+       int pid;
+       while(1){
+               a = (char*)calloc(1000*1000,sizeof(char));
+               if (a == NULL)
+                       printf("not enough memory\n");
+               for(i=0; i<1000*1000;i++)
+                       a[i]=(char)i;
+               usleep(100*1000);
+               k++;
+               if (k==3)
+                       pid =getuid();
+
+               printf("%dM used ---------\n",k);
+       }
+       return 0;
+}