codedeploy-agent is being reported as a unconfined daemon by AWS Inspector - aws-code-deploy

AWS Inspector reports:
1.6.1.6 Ensure no unconfined daemons exist
Severity
High
Description
Description Daemons that are not defined in SELinux policy will inherit the security context of their parent process. Rationale Since daemons are launched and descend from the init process, they will inherit the security context label initrc_t . This could cause the unintended consequence of giving the process more permission than it requires.
Recommendation
Investigate any unconfined daemons found during the audit action. They may need to have an existing security context assigned to them or a policy built for them.
Search for unconfined daemon:
[root#ip-10-0-1-136 bin]# ps -eZ | egrep "initrc" | egrep -vw "tr|ps|egrep|bash|awk" | tr ':' ' ' | awk '{ print $NF }'
ruby
ruby
I didn't find any document from AWS to confine codedeploy-agent. I was trying to follow a instruction from AWS to confine AWS Inspector agent instead. https://docs.aws.amazon.com/inspector/latest/userguide/inspector_installing-uninstalling-agents.html. ls -Z does show codedeploy-agent scripts in /etc/rc.d/init.d and /etc/init.d are with bin_t domain, however AWS Insprector scan still reports ruby (codedeploy-agent) a unconfined daemon process.
[root#ip-10-0-1-136 bin]# systemctl status codedeploy-agent.service
● codedeploy-agent.service - AWS CodeDeploy Host Agent
Loaded: loaded (/usr/lib/systemd/system/codedeploy-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-05-17 15:49:43 UTC; 7s ago
Process: 29048 ExecStop=/opt/codedeploy-agent/bin/codedeploy-agent stop (code=exited, status=0/SUCCESS)
Process: 29516 ExecStart=/bin/bash -a -c [ -f /etc/profile ] && source /etc/profile; /opt/codedeploy-agent/bin/codedeploy-agent start (code=exited, status=0/SUCCESS)
Main PID: 29528 (ruby)
CGroup: /system.slice/codedeploy-agent.service
├─29528 codedeploy-agent: master 29528
└─29532 codedeploy-agent: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller of master 29528
May 17 15:49:43 ip-10-0-1-136.eu-west-1.compute.internal systemd[1]: Starting AWS CodeDeploy Host Agent...
May 17 15:49:43 ip-10-0-1-136.eu-west-1.compute.internal systemd[1]: Started AWS CodeDeploy Host Agent.
[root#ip-10-0-1-136 bin]# ls -Z /opt/
drwxr-xr-x. root root system_u:object_r:usr_t:s0 aws
drwxr-xr-x. root root system_u:object_r:usr_t:s0 codedeploy-agent
[root#ip-10-0-1-136 bin]# ls -Z /opt/codedeploy-agent/bin/
-rwxr--r--. root root system_u:object_r:bin_t:s0 codedeploy-agent
-rwxr--r--. root root system_u:object_r:bin_t:s0 codedeploy-local
-rwxr--r--. root root system_u:object_r:bin_t:s0 install
-rwxr--r--. root root system_u:object_r:bin_t:s0 update
[root#ip-10-0-1-136 bin]# ls /etc/rc.d/init.d/ -Z
-rwxr-xr-x. root root system_u:object_r:bin_t:s0 awsagent
-rwxr--r--. root root system_u:object_r:bin_t:s0 codedeploy-agent
[root#ip-10-0-1-136 bin]# ls /etc/init.d/ -Z
-rwxr-xr-x. root root system_u:object_r:bin_t:s0 awsagent
-rwxr--r--. root root system_u:object_r:bin_t:s0 codedeploy-agent
Changed to bin_t seemed not working - since ps -Z still shows the process ran with initrc_t
[root#ip-10-0-1-136 bin]# ps -eZ | egrep "initrc" | egrep -vw "tr|ps|egrep|bash|awk" | tr ':' ' '
system_u system_r initrc_t s0 29528 ? 00 00 00 ruby
system_u system_r initrc_t s0 29532 ? 00 00 00 ruby

I have no idea how "correct" is my answer, but this what worked for me.
I guess that because bash actually launches it cannot inherit context correctly.
I found codedeploy-agent.service file. It contains line:
ExecStart=/bin/bash -a -c '[ -f /etc/profile ] && source /etc/profile; /opt/codedeploy-agent/bin/codedeploy-agent start'
I modified it to:
ExecStart=/opt/codedeploy-agent/bin/codedeploy-agent start
Do systemctl daemon-reload
sed command to perform replacement automatically:
sudo sed -i 's/ExecStart=.*/ExecStart=\/opt\/codedeploy-agent\/bin\/codedeploy-agent start/' /usr/lib/systemd/system/codedeploy-agent.service
And stop / start service
Option 2. This does not modifies files, but changes how do you start service. Do not start using systemctl, but instead with command:
/etc/rc.d/init.d/codedeploy-agent start
Make sure its stopped of course beforehand

Related

Using rsync with gitlab-ci how to deploy identically?

when I push and that, on my server, I don't have my project, everything is fine (obviously):
rsync --exclude=".git" -e ssh -avz --delete-after . $SSH_USER#$SSH_HOST:blog_symfony/
building file list ... done
created directory blog_symfony
[...]
sent 44,533,927 bytes received 5,523 bytes 5,239,935.29 bytes/sec
total size is 238,959,003 speedup is 5.37
the problem when I push a 2nd time, it does anything to me:
rsync: [generator] delete_file: rmdir(project/blog_symfony/project/blog_symfony) failed: Permission denied (13)
rsync: [generator] delete_file: rmdir(project/blog_symfony) failed: Permission denied (13)
deleting project/blog_symfony/translations/.gitignore
deleting project/blog_symfony/translations/
[...]
it creates for me, on my server side, a 'project' folder in the blog_symfony folder
annot delete non-empty directory: project/blog_symfony
cannot delete non-empty directory: project
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1207) [sender=3.1.3]
sent 13,924 bytes received 175 bytes 28,198.00 bytes/sec
total size is 238,959,004 speedup is 16,948.65
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1
my gitlab-ci:
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config'
script:
- ls
- apt-get update && apt-get install rsync -y
- ssh $SSH_USER#$SSH_HOST "ls"
- rsync --exclude=".git" -e ssh -avz --delete-after . $SSH_USER#$SSH_HOST:blog_symfony/
- ssh $SSH_USER#$SSH_HOST "cd blog_symfony && docker-compose build && docker-compose up"
in ls -l I have a folder written by rsync and which is impossible to remove from gitlab-ci:
drwxrwxr-x 3 root root 4096 Dec 14 23:26 project
I don't think this is normal. This is the first time that I use gitlab-ci for a symfony project.
Thank you for your help
ls -l: I have a folder written by rsync and which is impossible to remove from GitLab CI.
Check if that folder is instead created after the first execution of your docker-compose up: if your Docker image execute itself internally as USER root, using a bind mount, it would write files/folders as root.
And that would impede normal operation (on the server, outside the container), like your rsync, because root files would be i the way.

Is it possible to remove the prompt history on MariaDB?

The main problem is that I wrote a security sensitive statement on MariaDB (among many other statements), so I would like to remove the prompt history (like the command history -c on Linux):
jordiba90#lts:~$ sudo mariadb -u root -p
[sudo] contraseña para jordiba90:
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 36
Server version: 10.3.25-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> UPDATE user SET password=PASSWORD("PASSWORD") WHERE user="root";
Query OK, 0 rows affected (0.001 sec)
Rows matched: 1 Changed: 0 Warnings: 0
MariaDB [mysql]> FLUSH privileges;
Query OK, 0 rows affected (0.001 sec)
So, when I click the up&down buttons on my keyboard I see all the prompt history.
When I try to remove the prompt history on MariaDB, it does not work:
MariaDB [(none)]> clear
MariaDB [(none)]> \c
MariaDB [(none)]> quit
Bye
So, I have checked on the internet this script (that does not work either):
jordiba90#lts:~$ rm ~/.mysql_history
jordiba90#lts:~$ export MYSQL_HISTFILE=/dev/null
jordiba90#lts:~$ set | grep MYSQ
MYSQL_HISTFILE=/dev/null
_=MYSQL_HISTFILE=/dev/null
REFERENCE > https://www.thegeekstuff.com/2010/01/disable-mysql-history-clear-mysql_history-and-mysql_histfile/
After that, I have also checked on the internet another script (that does not work either, again):
jordiba90#lts:~$ ~/.mysql_history
bash: /home/jordiba90/.mysql_history: No existe el archivo o el directorio
jordiba90#lts:~$ rm $HOME/.mysql_history
rm: no se puede borrar '/home/jordiba90/.mysql_history': No existe el archivo o el directorio
jordiba90#lts:~$ ln -s /dev/null $HOME/.mysql_history
REFERENCE > https://www.cyberciti.biz/faq/howto-clear-mysql-command-history/
One the one hand, there are too many files on my OS related to "mysql" but none related to mysql history, so I have checked one by one all the files related to mysql configuration and I did not find the one I should remove:
jordiba90#lts:~$ locate mysql | wc
421 421 25918
jordiba90#lts:~$ locate mysql | grep history
jordiba90#lts:~$ locate mysql | grep conf
/etc/mysql/conf.d
/etc/mysql/conf.d/mysql.cnf
/etc/mysql/conf.d/mysqldump.cnf
/etc/mysql/mariadb.conf.d
/etc/mysql/mariadb.conf.d/50-client.cnf
/etc/mysql/mariadb.conf.d/50-mysql-clients.cnf
/etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf
/etc/mysql/mariadb.conf.d/50-server.cnf
/usr/share/mysql-common/configure-symlinks
/usr/share/mysql/systemd/use_galera_new_cluster.conf
/var/lib/dpkg/info/mysql-common.conffiles
One the other hand, the same happens with the mariadb files:
jordiba90#lts:~$ locate mariadb | wc
101 101 5042
jordiba90#lts:~$ locate mariadb | grep history
jordiba90#lts:~$ locate mariadb | grep conf
/etc/insserv.conf.d/mariadb
/etc/mysql/mariadb.conf.d
/etc/mysql/mariadb.conf.d/50-client.cnf
/etc/mysql/mariadb.conf.d/50-mysql-clients.cnf
/etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf
/etc/mysql/mariadb.conf.d/50-server.cnf
/usr/lib/systemd/system/mariadb#bootstrap.service.d/use_galera_new_cluster.conf
/var/lib/dpkg/info/mariadb-client-10.3.conffiles
/var/lib/dpkg/info/mariadb-common.conffiles
/var/lib/dpkg/info/mariadb-server-10.3.conffiles
/var/lib/dpkg/info/mariadb-server-10.3.config
Could you please give me a hint? You can check that I have tried it. Thanks in advance.
PS_1: if there is any other entry about this topic on Stack* (I have checked it and I did not see one) or if my entry breaks any rule on Stack* (I have checked the rules and I think I do not break any), please send me a PM and I will delete this entry. I am trying to learn without losing reputation points for asking a question.
PS_2: I have restarted mysql and right now the status is active (running):
jordiba90#lts:~$ sudo /etc/init.d/mysql restart
Restarting mysql (via systemctl): mysql.service.
jordiba90#lts:~$ sudo /etc/init.d/mysql status
● mariadb.service - MariaDB 10.3.25 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-11-14 13:24:47 CET; 8h ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 826 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
Process: 861 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 876 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
Process: 1026 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 1028 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
Main PID: 957 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 9423)
Memory: 93.1M
CGroup: /system.slice/mariadb.service
└─957 /usr/sbin/mysqld
nov 14 13:24:46 lts systemd[1]: Starting MariaDB 10.3.25 database server...
nov 14 13:24:47 lts mysqld[957]: 2020-11-14 13:24:47 0 [Note] /usr/sbin/mysqld (mysqld 10.3.25-MariaDB-0ubuntu0.20.04.1) starting as…ess 957 ...
nov 14 13:24:47 lts mysqld[957]: 2020-11-14 13:24:47 0 [Warning] Could not increase number of max_open_files to more than 16384 (request: 32186)
nov 14 13:24:47 lts systemd[1]: Started MariaDB 10.3.25 database server.
nov 14 13:24:47 lts /etc/mysql/debian-start[1030]: Upgrading MySQL tables if necessary.
nov 14 13:24:47 lts /etc/mysql/debian-start[1033]: Looking for 'mysql' as: /usr/bin/mysql
nov 14 13:24:47 lts /etc/mysql/debian-start[1033]: Looking for 'mysqlcheck' as: /usr/bin/mysqlcheck
nov 14 13:24:47 lts /etc/mysql/debian-start[1033]: This installation of MySQL is already upgraded to 10.3.25-MariaDB, use --force if y…l_upgrade
Hint: Some lines were ellipsized, use -l to show in full.
REFERENCE > https://superuser.com/questions/282115/how-to-restart-mysql
PS_3: I have tried the command stat ~/.mysql_history; rm ~/.mysql_history; stat ~/.mysql_history and the output that I have is that the file or the directory /home/jordiba90/.mysql_history does not exist
PS_4: I have tried the command strace mysql and I can not share here the output because there is a message body limit to 30.000 characters (and if I do that, I should have entered 44.321, aprox)
PS_5: Having checked the specific output openat(AT_FDCWD..., there is none related to mysql_history. It does not appear that specific sentence
PS_6: I have used 'sudo' to try the command 'strace mysql'and I have got the following output related to mysql history:
openat(AT_FDCWD, "/root/.mysql_history", O_RDONLY) = 4
fstat(4, {st_mode=S_IFREG|0600, st_size=4585, ...}) = 0
read(4, "misql -u root -p\nDatascience2005"..., 4585) = 4585
close(4) = 0
write(1, "\33(B\33[m\33(B\33[0;1mType 'help;' or '"..., 94Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
) = 94
write(1, "\n", 1
)
PS_7: I have removed the last directory related to mysql history named /root/.mysql_history but I still see the history on the mariadb prompt
Editing the history file (for example with vim, and deleting the problematic lines) worked in my case:
vim ~/.mysql_history
(mysql Ver 15.1 Distrib 10.4.14-MariaDB)
Depending on how you are set up, you'll probably find it is located at root. I had the same on a new secured install of MariaDB on Ubuntu 22.04. Mine was located at /root/.mysql_history
I found it the usual way by using the excellent locate command (remember to use the updatedb command to update the database of any new files that have been added) and searching for mysql_history:
locate -i mysql_history
Do this search at the bash (or whatever you are using) command prompt as you are searching for a file in the file system, not in the MariaDB or MySQL shell itself.
On this search, there is no actual need for the -i option as mysql_history is lowercase, but you never know!
Searching for history will also work but you'll get a few more hits.
Once you have found the file, you can use the editor of your choice to remove any sensitive command lines that may have found their way in there.

Accidentally used -S when creating my tmux session instead of -s, where is my session?

I created a new session of tmux, intending to use the -s flag to name it, but confused the command with -S which specifies a socket. I know the session is alive, because the processes I started in it are still running. But when I try to list running sessions it does not appear. Where is my session and how do I recover it?
In tmux option -S means socket path (from man tmux):
-S socket-path
Specify a full alternative path to the server socket. If -S is specified, the default
socket directory is not used and any -L flag is ignored.
So you have to find out the parent directory where tmux was run with
-S option and pass this option to tmux to point it to the
alternative socket path. You could for example find PID of the
tmux process:
$ ps aux | grep '[t]mux'
ja 15121 0.0 0.0 20252 2236 pts/6 S+ 00:44 0:00 tmux -S new
And then check this process cwd in /proc like this:
$ ls -l /proc/15121/cwd
lrwxrwxrwx 1 ja users 0 Aug 19 00:52 /proc/15121/cwd -> /home/ja
And then to refer to this tmux session:
$ tmux -S /home/ja/new ls
0: 1 windows (created Sat Aug 19 00:44:46 2017) [212x65] (attached)

Deploying ASP.NET 4.* MVC and Web API applications to Linux server

Is there any way of Deploying ASP.NET 4.* MVC and Web API applications to Linux server? I searched and read about Docker but I think that is for .NET 5.
Appreciate your help!
I managed to do that with some bricolage on a Linux-based Docker image.
This helped me a lot: https://github.com/junalmeida/docker-mono-web
Basically, you can host your web application with nginx, but you also need fastcgi-mono-server4.
The dockerfile looks like this:
FROM mono:latest
RUN apt-get update \
&& apt-get install -y \
iproute2 supervisor ca-certificates-mono fsharp mono-vbnc nuget \
referenceassemblies-pcl mono-fastcgi-server4 nginx nginx-extras \
&& rm -rf /var/lib/apt/lists/* /tmp/* \
&& echo "daemon off;" | cat - /etc/nginx/nginx.conf > temp && mv temp /etc/nginx/nginx.conf \
&& sed -i -e 's/www-data/root/g' /etc/nginx/nginx.conf
# this copies nginx configuration files to the proper directory
COPY nginx/ /etc/nginx/
In the link above, the supervisord command is used to start both nginx and fastcgi-mono-server4.
It is configured in a supervisord.conf file like the following, in which the --appconfigdir specifies the root folder of your application:
[supervisord]
logfile=/var/log/supervisor/supervisord.log
logfile_maxbytes = 50MB
nodaemon=true
user=root
[program:mono]
command=fastcgi-mono-server4 --appconfigdir=appconfig --socket=unix --filename=/var/run/mono-fastcgi.sock --printlog --name=mono
user=root
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=nginx
user=root
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
and launched with this command:
/usr/bin/supervisord -c supervisord.conf
(a folder contains a configuration for nginx, too).

nginx.conf and nginx.pid users and permissions

I'm embarking on watching my NGINX error.log files at level: warn... probably a silly idea and will cause me to crash my server as I work out any bugs happening, but hey, we're nerds and this is why we're here.
I'm noticing a [warn] and an [emerg] pop up every time I restart my server, which shows:
[warn] 8041#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
[emerg] 8041#0: open() "/run/nginx.pid" failed (13: Permission denied)
The top of my nginx.conf file reads:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
Which to me, shows me a few things.
I'm running NGINX with the user: www-data.
The number of worker processes that are allowed is automatically adjusted.
my PID file/information is being stored in /run/nginx.pid.
The error tells me that NGINX doesn't have permission to access /run/nginx.pid, which led me to see the user permissions for said file.
sudo ls -la /run/nginx.pid
reveals:
-rw-r--r-- 1 root root 5 Jun 18 05:34 /run/nginx.pid
Then trying:
ps -ef | grep nginx
produces:
root 5914 1 0 05:34 ? 00:00:00 nginx: master process /u
www-data 5917 5914 0 05:34 ? 00:00:00 nginx: worker process
scratches head
Now, can somebody out there tell me why, or how the hell NGINX has managed to create the master process with root ownership, and now the worker processes are owned by www-data?
Or more to the point, anybody have some suggestions on what to do about this [emerg] error I'm getting?
My first thought is to just try and change the ownership of the /run/nginx.pid file and see how NGINX likes it, but I kind of feel that even if I do that manually this time, when I restart the server, I'll run into the same problem.
My second thought is maybe there is somewhere else that I define my worker process initiation within NGINX..
Thanks.
EDIT
The contents of the /etc/systemd/system/multi-user.target.wants/nginx.service file are:
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/usr/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
I got the same error on my Centos 7 server today.
nginx.pid" failed (13: Permission denied)
For me, it turned out to be a problem with SELinux. I did the following to make it work again:
systemctl stop nginx
touch /var/run/nginx.pid
chcon -u system_u -t httpd_var_run_t /var/run/nginx.pid
systemctl start nginx
running
ls -Z nginx.pid
should output
-rw-r--r--. root root system_u:object_r:httpd_var_run_t:s0 nginx.pid
In my case I got a
"/usr/local/var/run/nginx.pid" failed (13: Permission denied)
bind() to 0.0.0.0:80 failed (48: Address already in use)
and the working solution was made up of these steps:
stop root process
sudo nginx -s stop
check if process stopped
ps aux | grep nginx
restart process
sudo nginx -s reload
gave me the error
nginx: [error] open() “/usr/local/var/run/nginx.pid” failed (2: No such file or directory)
probabil .pid was started with the wrong root user as I uncommented the line with path to .pid in /usr/local/etc/nginx/nginx.conf and then I commented it back again
to start nginx as a user and not root
brew services start nginx
result at running command
ps aux | grep nginx
youruser 89212 0.0 0.0 4268280 644 s002 S+ 2:46PM 0:00.00 grep nginx
youruser 89179 0.0 0.0 4302204 1776 ?? S 2:45PM 0:00.00 nginx: worker process
youruser 89178 0.0 0.0 4275372 4368 ?? S 2:45PM 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
And as it can be seen, the nginx process started with the expected user and not as root and the conflict between processes was gone and I could access the PHP application local domain.
For Ubuntu 20.04+
I got the exact same error while I was using sudo systemctl reload nginx
Using sudo service nginx restart instead throws no error

Resources