Monday, December 15, 2014

19-бүлэг. Төрөл, илэрхийлэл

Энэ бүлэгт С++ хэл дэх төрлийн систем, түүнчлэн илэрхийлэл хэрхэн үнэлэгдэж, хувиргагддаг талаар гүнзгийрүүлэн авч үзэх болно.

Юуны түрүүнд цаашид хэрэглэгдэх зарим хэллэгүүдийн товч тодорхойлолтыг өгье.
Оператор гэдэг нь функцийн тусгай нэгэн төрөл бөгөөд өгөгдсөн операнд дээр тодорхой үйлдлийг гүйцэтгэж, үр дүнг нь буцаана. Операнд гэдэг нь аль нэг операторын аргумент юм.

Ерөнхийдөө оператор нь энгийн функц бөгөөд зарим тохиолдолд эдгээр функцийг инфикс үйлдлийн тэмдэгтэйгээр (+, -, *, / зэрэг) дуудаж болно гэж ойлгох нь зохимжтой. Өөрөөр хэлбэл функц дуудах урт бичлэгтэй синтаксийн (sum = operator+(a, b);) оронд уншихад илүү дөхөм инфикс синтаксыг (sum = a + b;) ашиглаж болно.

Илэрхийлэл нь нэг эсвэл олон тооны операнд болон фунцкуудээс тогтоно. Илэрхийлэл бүр тодорхой төрөл, мөн утгатай байна. Уг утга нь тухайн өгөгдсөн операнд дээр операторын тодорхойлолтод заасан үйлдлийг гүйцэтгэснээр гарч ирнэ.

Monday, March 12, 2012

Customizing SELinux policy

Problem: when to upload a file to TFTP server, which runs on Linux host (i.e., Fedora guest), SELinux denies this access and reports it to AVC audit file in "/var/log/audit/audit.log". It happens, because any request of tftp daemon on specified target directory and file are not allowed in the SELinux policy.

Solution: to resolve this issue, a corresponding custom policy module can be created and added to SELinux policy with the "audit2allow" tool.

Case: for example, consider the following AVC audit log, after invoking following command from a remote Windows host:

"tftp -i 192.168.0.120 PUT installed_apps.txt"

----------------------------------------------------------------------

Summary:

SELinux is preventing in.tftpd (tftpd_t) "search" to ./mnt (mnt_t).

Detailed Description:

SELinux denied access requested by in.tftpd. It is not expected that this access
is required by in.tftpd and this access may signal an intrusion attempt. It is
also possible that the specific version or configuration of the application is
causing it to require additional access.

Allowing Access:

Sometimes labeling problems can cause SELinux denials. You could try to restore
the default system file context for ./mnt,

restorecon -v './mnt'

If this does not work, there is currently no automatic way to allow this access.
Instead, you can generate a local policy module to allow this access - see FAQ
(http://fedora.redhat.com/docs/selinux-faq-fc5/#id2961385) Or you can disable
SELinux protection altogether. Disabling SELinux protection is not recommended.
Please file a bug report (http://bugzilla.redhat.com/bugzilla/enter_bug.cgi)
against this package.

Additional Information:

Source Context system_u:system_r:tftpd_t:s0-s0:c0.c1023
Target Context system_u:object_r:mnt_t:s0
Target Objects ./mnt [ dir ]
Source in.tftpd
Source Path /usr/sbin/in.tftpd
Port
Host efe1272.efe
Source RPM Packages tftp-server-0.49-1.fc10
Target RPM Packages filesystem-2.4.19-1.fc10
Policy RPM selinux-policy-3.5.13-74.fc10
Selinux Enabled True
Policy Type targeted
MLS Enabled True
Enforcing Mode Enforcing
Plugin Name catchall_file
Host Name efe1272.efe
Platform Linux efe1272.efe 2.6.27.41-170.2.117.fc10.i686 #1
SMP Thu Dec 10 11:00:29 EST 2009 i686 i686
Alert Count 25
First Seen Mon 12 Mar 2012 09:21:50 AM CET
Last Seen Mon 12 Mar 2012 01:30:02 PM CET
Local ID e48f9047-a5b0-4f62-8510-d57f015c60c1
Line Numbers

Raw Audit Messages

node=efe1272.efe type=AVC msg=audit(1331555402.558:42): avc: denied { search } for pid=12268 comm="in.tftpd" name="mnt" dev=sda3 ino=1036321 scontext=system_u:system_r:tftpd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:mnt_t:s0 tclass=dir

node=efe1272.efe type=SYSCALL msg=audit(1331555402.558:42): arch=40000003 syscall=12 success=no exit=-13 a0=bfe35e81 a1=1 a2=4f5dec4a a3=bfe34734 items=0 ppid=2289 pid=12268 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="in.tftpd" exe="/usr/sbin/in.tftpd" subj=system_u:system_r:tftpd_t:s0-s0:c0.c1023 key=(null)

By running "audit2allow" on above alert and reviewing the resultant "mntfixlocal.te" policy file will show us (as root):

---------------------------------------------------------------------------

# grep mnt_t /var/log/audit/audit.log | audit2allow -M mntfixlocal
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i mntfixlocal.pp

# cat mntfixlocal.te

module mntfixlocal 1.0;

require {
type tftpd_t;
type mnt_t;
class dir search;
}

#============= tftpd_t ==============
allow tftpd_t mnt_t:dir search;

----------------------------------------------------------

To compile and load this policy module invoke following command (as root):

-----------------------------------------------------------

# semodule -i mntfixlocal.pp

-----------------------------------------------------------

To make sure that the custom policy module is activated, look for any policies regarding "tftpd_t" and "mnt_t" (as user):

-----------------------------------------------------------

$ sesearch -A | grep tftpd_t | grep mnt_t
WARNING: This policy contained disabled aliases; they have been removed.
allow tftpd_t mnt_t : dir search ;

------------------------------------------------------------

Alternatively, we can edit the custom policy module .te file to prevent auditing of such alert whilst still allowing SELinux to continue preventing access. We do this by changing the "allow" line to "dontaudit":

------------------------------------------------------------

#============= tftpd_t ==============
dontaudit tftpd_t mnt_t:dir search;

------------------------------------------------------------

This way we can change any alerts to custom policy modules and add them to SELinux policy.

Finally our custom policy module will be shown as follows (as user):

-----------------------------------------------------------

$ sesearch -A | grep tftpd_t | grep vmblock_t
WARNING: This policy contained disabled aliases; they have been removed.
allow tftpd_t vmblock_t : file { read write create getattr } ;
allow tftpd_t vmblock_t : dir { write add_name search } ;
-------------------------------------------------------------

Source: http://wiki.centos.org/HowTos/SELinux
and also: http://serverfault.com/questions/106007/tftp-uploads-failing


Tuesday, October 4, 2011

WLAN settings on Nokia N95

To define access point:

Menu -> Tools -> Settings -> Connection -> Access points

- Connection name =
- Data bearer = Wireless LAN
- WLAN network name =
- Network status = Public
- WLAN network mode = Infrastructure
- WLAN security mode = WPA/WPA2 (must be compatible with your WLAN router)
- WLAN security settings
-- WPA/WPA2 = Pre-shared key
-- Pre-shared key = (key phrase used in your WLAN)
-- WPA2 only mode = Off

To activate the access point:

Menu -> Web -> Options -> Settings -> General :
- Access point = Always ask

Nokia N95 MAC address

Dial
*#MAC WLAN#
or
*#62209526#

Friday, December 3, 2010

Public key usage on Thunderbird (with Enigmail OpenPGP Key Manager)

The Enigmail OpenPGP Key Manager is designed to be simple to use with a clean, powerful interface.

You can access it from within Thunderbird/SeaMonkey via the OpenPGP menu: OpenPGP > Key Management.
You can also run it as a stand alone application by appending -pgpkeyman to the command that runs Thunderbird/SeaMonkey, e.g. on Windows: "C:\Program Files\Mozilla Thunderbird\thunderbird.exe" -pgpkeyman.

For more details refer here.

Wednesday, March 17, 2010

Enable "Large File Support" in uClibc

Sources:

[1] Blackfin uClinux (http://docs.blackfin.uclinux.org/doku.php?id=faq)

Steps:

1. Jump to a toolchain source folder

cd /opt/ifx-lxdb-1-2


2. Delete ".configured" file (it was created once the toolchain built)

rm -f .configured


3. Enable LFS (Large File Support) in the configuration files

cd source/uclibc
chmod 666 ifx-uClibc-def-config.mips
chmod 666 ifx-uClibc-opt-config.mips


Replace
# UCLIBC_HAS_LFS is not set

with
UCLIBC_HAS_LFS=y

in both files.

4. Return to the top folder and build the toolchain again.

cd ../..
./setup.sh all

Strace build on mips with uClibc

Sources:

[1] Building Embedded Linux Systems, pp. 297-298 (how to build strace)
[2] Blackfin uClinux (important tips)
[3] Assabet (compiling strace)
[4] Unable to build strace - busybox forum (same build error and reason of the error)

Steps:

1. Download strace package (strace-4.5.19.tar.bz2, 499,6KB) from sourceforge (http://sourceforge.net/projects/strace/) and save it under "~/debug" folder on a host [1].

2. Jump to the folder and unpack the package

cd ~/debug
tar -xjf strace-4.5.19.tar.bz2

(it will be a folder named "strace-4.5.19" created)

3. Jump to the recently created folder

cd strace-4.5.19


4. Configure and build it against uClibc [1]

CC=mips-linux-gcc ./configure --host=mips-linux
make LDFLAGS="-static"


5. First time it will return error messages as shown below:

file.c: In function `printstatfs64':
file.c:1695: error: storage size of `statbuf' isn't known
file.c:1695: warning: unused variable `statbuf'
file.c: In function `sys_statfs64':
file.c:1729: error: invalid application of `sizeof' to an incomplete type
file.c: In function `sys_fstatfs64':
file.c:1743: error: invalid application of `sizeof' to an incomplete type
make[1]: *** [file.o] Error 1
make[1]: Leaving directory `/home/boldoo/debug/strace-4.5.19'
make: *** [all] Error 2


6. By searching on the Internet [4], the build failure reason results from not supporting large file (Large File Support, LFS) in uClibc. So decided to modify sources that references following functions:

sys_truncate64()
sys_ftruncate64()
sys_getdents64()
sys_statfs64()
sys_fstatfs64()


Two files found that contain the functions:
file.c
linux/mips/syscallent.h


7. All lines that include 64-bit functions were commented in both source files.

8. Clean and build again.

make clean
make LDFLAGS="-static"


Build passed without any error.

9. Copy strace executable to root filesystem [1]

cp strace /opt/rootfs.incaip2/sbin/


10. Run minicom, connect a target board (INCAIP2) serially to the host and turn on the target board.

11. After booting Linux on the target, check if strace works:

# strace ls
execve("/bin/ls", ["ls"], [/* 11 vars */]) = 0
ioctl(0, TIOCNXCL, {B115200 opost isig icanon echo ...}) = 0
ioctl(1, TIOCNXCL, {B115200 opost isig icanon echo ...}) = 0
getuid() = 0
getgid() = 0
setgid(0) = 0
setuid(0) = 0
ioctl(1, 0x40087468, 0x7fff7cc0) = 0
ioctl(1, TIOCNXCL, {B115200 opost isig icanon echo ...}) = 0
lstat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
brk(0) = 0x1000a120
brk(0x1000b120) = 0x1000b120
brk(0x1000c000) = 0x1000c000
stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open(".", O_RDONLY) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
getdents(3, /* 7 entries */, 3933) = 128
lstat("./drv_vmmc", {st_mode=S_IFREG|0755, st_size=345000, ...}) = 0
lstat("./hapi.o", {st_mode=S_IFREG|0664, st_size=10996, ...}) = 0
lstat("./tmp", {st_mode=S_IFLNK|0777, st_size=22, ...}) = 0
lstat("./drv_tapi", {st_mode=S_IFREG|0755, st_size=171132, ...}) = 0
lstat("./startpa.sh", {st_mode=S_IFREG|0755, st_size=246, ...}) = 0
getdents(3, /* 0 entries */, 3933) = 0
close(3) = 0
open("/etc/TZ", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/TZ", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/TZ", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/TZ", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/TZ", O_RDONLY) = -1 ENOENT (No such file or directory)
write(1, "drv_tapi drv_vmmc hapi.o "..., 52drv_tapi drv_vmmc hapi.p
) = 52
exit(0) = ?


12. Strace the "voip" application

# strace voip
execve("/usr/local/bin/voip", ["voip"], [/* 11 vars */]) = 0
old_mmap(NULL, 20, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x0
stat("/etc/ld.so.cache", {st_mode=S_IFREG|0644, st_size=64, ...}) = 0
open("/etc/ld.so.cache", O_RDONLY) = 3
old_mmap(NULL, 64, PROT_READ, MAP_SHARED, 3, 0) = 0x2aaaf000
close(3) = 0
open("/lib/libc.so.0", O_RDONLY) = 3
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0
read(3, "\177ELF\1\2\1\0\0\0\0\0\0\0\0\0\0\3\0\10\0\0\0\1\0\0\265\0\0\0\0004"..6
old_mmap(NULL, 684032, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2aaee000
old_mmap(0x2aaee000, 397556, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0) 0
old_mmap(0x2ab8f000, 4748, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x610
old_mmap(0x2ab91000, 15800, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANO0
close(3) = 0
munmap(0x2aab0000, 4096) = 0
munmap(0x2aaaf000, 64) = 0
ioctl(0, TIOCNXCL, {B115200 opost isig icanon echo ...}) = 0
ioctl(1, TIOCNXCL, {B115200 opost isig icanon echo ...}) = 0
write(1, "voip application (Mar 12 2010, 1"..., 42voip application (Mar 12 2010)

) = 42
rt_sigaction(SIGINT, {0x14000000, [RT_68], 0x400b80 /* SA_??? */}, {SIG_DFL, [R0
open("/dev/vmmc10", O_RDWR) = 3
open("/dev/vmmc11", O_RDWUnable to handle kernel paging request at virtual addr4
Oops in fault.c::do_page_fault, line 226:
$0 : 00000000 802e0000 fffffffc fffffffc 00000000 83791d4c 20004f11 83791d4c
$8 : 00000000 8027fb38 00000000 00000000 00000007 00000020 836c66b0 00000013
$16: 00000000 ffffffea 83791d4c 806e4f00 00000001 c0080fb0 83707000 836c4000
$24: 00000000 00432050 83790000 83791c80 00000000 802aa1b4
Hi : 00000000
Lo : 00000002
epc : 802ac5c8 Not tainted
Status: 1000fd03
Cause : 00800008
PrId : 00019640
Process voip (pid: 66, stackpage=83790000)
Stack: 806a4a3c 801cf2c4 83a09000 80309178 00000000 802aa1b4 2ab420d0
00000fff 801cf474 839a7550 83e25e80 00000001 83e25e80 00000001 00000000
80118e78 00000000 00001000 00000000 80118ad0 00000000 80118ad0 80103550
810e6c00 c00869cc 83e25e80 80307c00 83e25e80 00000060 83791d40 00000000
00000000 00000000 c00d9cb0 83791dd0 00000000 00000000 c0080fb0 c00b8b90
00000102 ...
Call Trace: [< 801cf2c4 >] [< 802aa1b4 >] [< 801cf474 >] [< 80118e78 >] [< 80118ad0 >]
[< 80118ad0 >] [< 80103550 >] [< c00869cc >] [< c00d9cb0 >] [< c0080fb0 >] [< c00b8b90 >]
[< c0080fb0 >] [< 80134094 >] [< c00d9cb0 >] [< c00d9d20 >] [< c009d610 >] [< c0080f14 >]
[< 80126a98 >] [< 80131438 >] [< 80131438 >] [< c0077540 >] [< 8011e964 >] [< 80126b14 >]
[< 8011ea3c >] [< 8011ea34 >] [< 8010c180 >] [< 8010c004 >] [< c0080710 >] [< c0076e2c >]
[< 8014e0d8 >] [< 80108b4c >] [< 8014de38 >] [< 80109a60 >] [< 80161e4c >]

Code: 00021080 00441021 aca30004 < 8c460000 > 00061a02 00061402 00431021 004
R) = 4
brk(0) = 0x1000013c
brk(0x1000113c) = 0x1000113c
brk(0x10002000) = 0x10002000
ioctl(4, 0x2000710f
+++ killed by SIGSEGV +++
Segmentation fault