nodejs process.setgid, process.setuid behavior with fs module - node.js

directory:
drwxrwxr-x 2 alex alex 4096 Aug 3 12:03 ./
drwxr-xr-x 17 alex alex 4096 Aug 3 11:18 ../
-rwx------ 1 root root 19 Aug 3 11:24 privilegedStuff*
-rwxrwx--- 1 root root 28 Aug 3 12:10 privilegedStuff1*
-rwxrwxr-x 1 alex alex 830 Aug 3 12:12 test.js*
test.js:
#!/usr/bin/env node
var fs = require('fs');
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n switching user and group...\n');
process.setgid(1000);
process.setegid(1000);
process.setuid(1000);
process.seteuid(1000);
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n output: \n');
console.log(fs.readFileSync('./privilegedStuff1', 'utf8'))
// this throws error as expected so I commented that
// console.log(fs.readFileSync('./privilegedStuff', 'utf8'))
privilegedStuff1:
content of privilegedStuff1
result:
alex#hp:/apps/test$ sudo ./test.js
user id: 0
group id: 0
user effective id: 0
group effective id: 0
switching user and group...
user id: 1000
group id: 1000
user effective id: 1000
group effective id: 1000
output:
content of privilegedStuff1
so what I don't understand is why node doesn't throw an error as it does nicely with privilegedStuff file? What am I missing?
alex#hp:/apps/test$ groups
alex adm cdrom sudo dip plugdev lpadmin sambashare
alex#hp:/apps/test$ cat privilegedStuff1
cat: privilegedStuff1: Permission denied
alex#hp:/apps/test$ sudo -s
root#hp:/apps/test# groups
root

In my test, I don't have such problem.
Can you enter the following command and show the result:
ls -l privilegedStuff1
id

Related

Write to a sysfs node, causing the system always write to the node

I locally wirte a module to test function/feature, And I create follow node info:
/sys/class/dbc/dbc # ls -l
total 0
-rw------- 1 root root 4096 2021-10-08 21:52 dbc_backlight
-rw------- 1 root root 4096 2021-10-08 22:30 dbc_pwm_max
-rw------- 1 root root 4096 2021-10-08 22:30 dbc_pwm_min
-rw------- 1 root root 4096 2021-10-08 21:52 dbc_setting
-rw------- 1 root root 4096 2021-10-08 21:52 dbc_thread_enable
-r--r--r-- 1 root root 4096 2021-10-08 22:30 dev
drwxr-xr-x 2 root root 0 2021-10-08 22:30 power
lrwxrwxrwx 1 root root 0 2021-10-08 22:30 subsystem -> ../../../../class/dbc
-rw-r--r-- 1 root root 4096 2021-10-08 22:30 uevent
when I echo right value to dbc_backlight node, can normally work, but when I write error value to dbc_backlight node, will result always write, info is follow:
node source code is follow:
static ssize_t dbc_backlight_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int DBC_BACKLIGHT = 0;
int readCount = 0;
printk("===========Set DBC Backlight========\n");
readCount = sscanf(buf, "%d", &DBC_BACKLIGHT);
if (readCount != 1)
{
printk("[ERROR] cannot read DBC_BACKLIGHT from [%s] \n", buf);
return 0;
}
if (DBC_BACKLIGHT > 100)
{
printk("Invalid Parameter DBC_BACKLIGHT=%d \n", DBC_BACKLIGHT);
return 0;
}
printk("Set Parameter DBC_BACKLIGHT=%d success\n", DBC_BACKLIGHT);
m_u8BacklightSetting = DBC_BACKLIGHT;
SetActiveBacklightSwitch(m_eActiveBackLight, m_u8BacklightSetting);
return count;
}
abnormal status dmesg log info is:
[ 2562.416693] ===========Set DBC Backlight========
[ 2562.416739] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.416786] ===========Set DBC Backlight========
[ 2562.416832] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.416878] ===========Set DBC Backlight========
[ 2562.416960] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.417006] ===========Set DBC Backlight========
[ 2562.417089] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.417135] ===========Set DBC Backlight========
[ 2562.417181] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.417265] ===========Set DBC Backlight========
[ 2562.417309] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.417391] ===========Set DBC Backlight========
[ 2562.417436] Invalid Parameter DBC_BACKLIGHT=101
[ 2562.417481] ===========Set DBC Backlight========
[ 2562.417564] Invalid Parameter DBC_BACKLIGHT=101
the log will always running and can't stop, otherwise, kill -9 pid can kill(kill pid can't kill it), top info is follow:
Tasks: 410 total, 2 running, 349 sleeping, 0 stopped, 0 zombie
Mem: 1694992k total, 1583088k used, 111904k free, 12844k buffers
Swap: 409596k total, 13056k used, 396540k free, 732388k cached
400%cpu 6%user 102%nice 135%sys 157%idle 0%iow 0%irq 0%sirq 0%host
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
2272 logd 30 10 34M 9.4M 4.1M S 152 0.5 2:29.57 logd
10181 root 20 0 4.4M 2.3M 1.9M R 98.6 0.1 1:33.14 sh -
kill -9 10181 can stop thread running.
I don't know why always write the node(dbc_backlight), please help me.
And locally, I do follow modify, the problem will not reproduce:
printk("===========Set DBC Backlight========\n");
readCount = sscanf(buf, "%d", &DBC_BACKLIGHT);
if (readCount != 1)
{
printk("[ERROR] cannot read DBC_BACKLIGHT from [%s] \n", buf);
return 0;
}
if (DBC_BACKLIGHT > 100)
{
printk("Invalid Parameter DBC_BACKLIGHT=%d \n", DBC_BACKLIGHT);
return 0;
}
//modify follow will fix it the problem
printk("===========Set DBC Backlight========\n");
readCount = sscanf(buf, "%d", &DBC_BACKLIGHT);
if (readCount != 1)
{
printk("[ERROR] cannot read DBC_BACKLIGHT from [%s] \n", buf);
return -EINVAL; //........
}
if (DBC_BACKLIGHT > 100)
{
printk("Invalid Parameter DBC_BACKLIGHT=%d \n", DBC_BACKLIGHT);
return -EINVAL;........
}
Do you know why? thanks for your help.
On success, .store function should return number of characters written.
In fail, it should return negative error code.
Returning 0 (return 0;) from that function is incorrect.
As you correctly noted, you can use return -EINVAL; for indicate that input is invalid.

EJS Deployment Issue, "listnames are not defined"

I am referring to this document here: https://www.freecodecamp.org/news/develop-deploy-first-fullstack-web-app/#nextsteps
Yesterday with your help, I was able to successfully deploy a static website. now I am trying to deploy a dynamic website.I moved and named all my files as described in the article in the link.
my file structure is like this:
ls -l
total 52
drwxr-xr-x 3 debian debian 4096 Apr 18 02:55 Concept
drwxr-xr-x 67 debian debian 4096 Apr 19 22:50 node_modules
-rw-r--r-- 1 debian debian 305 Apr 19 22:49 package.json
-rw-r--r-- 1 debian debian 18488 Apr 19 22:49 package-lock.json
drwxr-xr-x 4 debian debian 4096 Apr 18 02:55 pictures
-rw-r--r-- 1 debian debian 568 Apr 20 00:01 server.js
-rw-r--r-- 1 debian debian 4621 Apr 18 02:55 styles.css
drwxr-xr-x 4 debian debian 4096 Apr 19 22:56 views
Then I have this
$ ls -l views/pages
total 8
-rw-r--r-- 1 debian debian 5231 Apr 19 23:40 index.ejs
And also this
$ ls -l views/partials
total 4
-rw-r--r-- 1 debian debian 1083 Apr 19 23:05 so_header.ejs
The content of server.js is this:
// Load Node modules
var express = require('express');
const ejs = require('ejs');
// Initialize Express
var app = express();
// Render static files
app.use(express.static('/home/debian/public'));
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Port website will run on
app.listen(8080);
// *** GET Routes - Display Pages ***
// Root Route
app.get('/', function(req, res){
var listnames = ["Aras", "Songul", "Nafiz"];
// Render index page
res.render('/home/debian/public/views/pages/index.ejs', {
// EJS variable and server side variable
});
});
So when I initialize the server with npm start command and from my browser when I write and hit enter
http://myVpsIP:8080/
it gives an error something like this:
ReferenceError: /home/debian/public/views/pages/index.ejs:44
42|
43| <% include('/home/debian/public/views/partials/so_header') %>
>> 44| <% listnames.forEach(function(name){ %>
45| <p><%= name %></p>
46| <% }); %>
47|
listnames is not defined
at eval (eval at compile (/home/debian/public/node_modules/ejs/lib/ejs.js:662:12), <anonymous>:15:8)
at index (/home/debian/public/node_modules/ejs/lib/ejs.js:692:17)
at tryHandleCache (/home/debian/public/node_modules/ejs/lib/ejs.js:272:36)
at View.exports.renderFile [as engine] (/home/debian/public/node_modules/ejs/lib/ejs.js:489:10)
at View.render (/home/debian/public/node_modules/express/lib/view.js:135:8)
at tryRender (/home/debian/public/node_modules/express/lib/application.js:640:10)
at Function.render (/home/debian/public/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/debian/public/node_modules/express/lib/response.js:1012:7)
at /home/debian/public/server.js:17:6
at Layer.handle [as handle_request] (/home/debian/public/node_modules/express/lib/router/layer.js:95:5)
listnames are already defined in server.js and I am not really sure why it gives this error. Any help would be appreciated.
You should pass the listnames local variable to res.render() method like this:
res.render('/home/debian/public/views/pages/index.ejs', {
listnames
});
Then you can use listnames variable in your EJS template.

Filebeat only harvests some of the csv files

I have configured filebeat to send different (VoIP/SMS) csv files to logstash. However, only VoIP .csv files get shipped to logstash.
Csv files are under different folders.
logs/sms
logs/voip
I had another issue, described in this stack post. I managed to partially sort that out by creating tags in filebeat for these .csvs.
pwd
/usr/share/filebeat/logs
ls -ltr
drwxr-xr-x 2 root root 106496 Dec 4 03:39 sms
drwxr-xr-x 2 root root 131072 Dec 8 01:49 voip
ls -ltr voip | head -4
-rw-r--r-- 1 root root 7933 Dec 4 03:39 sms_cdr_1010.csv
-rw-r--r-- 1 root root 7974 Dec 4 03:39 sms_cdr_101.csv
-rw-r--r-- 1 root root 7949 Dec 4 03:39 sms_cdr_1009.csv
ls -ltr voip | head -4
-rw-r--r-- 1 root root 11616 Dec 4 03:39 voip_cdr_10.csv
-rw-r--r-- 1 root root 11533 Dec 4 03:39 voip_cdr_1.csv
-rw-r--r-- 1 root root 11368 Dec 4 03:39 voip_cdr_0.csv
Filebeat only starts harvesting voip .csvs
2019-12-08T02:37:18.872Z INFO crawler/crawler.go:72 Loading Inputs: 1
2019-12-08T02:37:18.872Z INFO log/input.go:138 Configured paths: [/usr/share/filebeat/logs/voip/*]
2019-12-08T02:37:18.872Z INFO input/input.go:114 Starting input of type: log; ID: 801046369164835837
2019-12-08T02:37:18.872Z INFO crawler/crawler.go:106 Loading and starting Inputs completed. Enabled inputs: 1
2019-12-08T02:37:18.977Z INFO log/harvester.go:255 Harvester started for file: /usr/share/filebeat/logs/voip/voip_cdr_185.csv
2019-12-08T02:37:18.978Z INFO log/harvester.go:255 Harvester started for file: /usr/share/filebeat/logs/voip/voip_cdr_2809.csv
2019-12-08T02:37:18.979Z INFO log/harvester.go:255 Harvester started for file: /usr/share/filebeat/logs/voip/voip_cdr_2847.csv
filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- logs/sms/*
tags: ["sms"]
paths:
- logs/voip/*
tags: ["voip"]
output.logstash:
enabled: true
hosts: ["logstash:5044"]
logging.to_files: true
logging.files:
logstash.conf
input {
beats {
port => "5044"
}
}
filter {
if "sms" in [tags] {
csv {
columns => ['Date', 'Time', 'PLAN', 'CALL_TYPE', 'MSIDN', 'IMSI', 'IMEI']
separator => ","
skip_empty_columns => true
quote_char => "'"
}
}
if "voip" in [tags] {
csv {
columns => ['Record_Nb', 'Date', 'Time', 'PostDialDelay', 'Disconnect-Cause', 'Sip-Status','Session-Disposition', 'Calling-RTP-Packets-Lost','Called-RTP-Packets-Lost', 'Calling-RTP-Avg-Jitter','Called-RTP-Avg-Jitter', 'Calling-R-Factor', 'Called-R-Factor', 'Calling-MOS', 'Called-MOS', 'Ingress-SBC', 'Egress-SBC', 'Originating-Trunk-Group', 'Terminating-Trunk-Group']
separator => ","
skip_empty_columns => true
quote_char => "'"
}
}
}
output {
if "sms" in [tags] {
elasticsearch {
hosts => ["elasticsearch:9200"]
index => "smscdr_index"
}
stdout {
codec => rubydebug
}
}
if "voip" in [tags] {
elasticsearch {
hosts => ["elasticsearch:9200"]
index => "voipcdr_index"
}
stdout {
codec => rubydebug
}
}
}
try below configuration,
filebeat.inputs:
- type: log
enabled: true
paths:
- /usr/share/filebeat/logs/sms/*.csv
tags: ["sms"]
paths:
- /usr/share/filebeat/logs/voip/*.csv
tags: ["voip"]
output.logstash:
enabled: true
hosts: ["logstash:5044"]
logging.to_files: true
logging.files:

How to determine if an SFTP file is a directory in Node.js?

The ssh2 library's SFTP readdir method gives me back all the files in the remote directory. How can I tell if any of of them are directories?
Here's some example output from the library:
{ filename:
'myfile',
longname:
'-rwxr-x--- 1 myuser mygroup 19036227 Nov 21 11:05 myfile',
attrs:
Stats {
mode: 33256,
permissions: 33256,
uid: 603,
gid: 1014,
size: 19036227,
atime: 1542859216,
mtime: 1542816340 } }
The file's mode contains bits indicating its type. You can check it like this:
const fs = require('fs');
function isDir(mode) {
return (mode & fs.constants.S_IFMT) == fs.constants.S_IFDIR;
}
isDir(myfile.attrs.mode);

simple logstash command is not working

It is working for auth.log but not working for authcopy.log. There is no error message. There is no output.
This is working.
sudo /usr/share/logstash/bin/logstash -e 'input { file { path => "/var/log/auth.log" } }'
output:
{
"#version" => "1",
"host" => "removed",
"path" => "/var/log/auth.log",
"#timestamp" => 2018-01-10T23:51:39.912Z,
"message" => "Jan 10 20:17:55 removed sudo: pam_unix(sudo:session): session closed for user root"
}
...
This is not working.
sudo /usr/share/logstash/bin/logstash -e 'input { file { path => "/var/log/authcopy.log" } }'
There is no error message. There is no output.
Copied auth.log to authcopy.log
sudo cp /var/log/auth.log /var/log/authcopy.log
sudo chmod 777 /var/log/authcopy.log
ls -l /var/log/auth*.log
-rwxrwxrwx 1 root root 391617 Jan 10 19:30 /var/log/authcopy.log
-rw-r----- 1 syslog adm 395465 Jan 10 20:13 /var/log/auth.log

Resources