1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 #include <files_properties.h>
26
27 #include <files_names.h>
28 #include <files_interfaces.h>
29 #include <item_lib.h>
30
31 static Item *SUSPICIOUSLIST = NULL; /* GLOBAL_P */
32
AddFilenameToListOfSuspicious(const char * pattern)33 void AddFilenameToListOfSuspicious(const char *pattern)
34 {
35 PrependItem(&SUSPICIOUSLIST, pattern, NULL);
36 }
37
38 static const char *const SKIPFILES[] =
39 {
40 ".",
41 "..",
42 "lost+found",
43 ".cfengine.rm",
44 NULL
45 };
46
47 /* TODO rework to accept only one param: path+nodename */
ConsiderFile(const char * nodename,const char * path,struct stat * stat)48 static bool ConsiderFile(const char *nodename, const char *path, struct stat *stat)
49 {
50 int i;
51
52 if (nodename[0] == '\0')
53 {
54 Log(LOG_LEVEL_ERR, "Empty (null) filename detected in '%s', skipping", path);
55 return false;
56 }
57
58 if (stat != NULL && (S_ISREG(stat->st_mode) || S_ISLNK(stat->st_mode)) &&
59 IsItemIn(SUSPICIOUSLIST, nodename))
60 {
61 Log(LOG_LEVEL_WARNING, "Filename '%s/%s' is classified as suspiscious, skipping", path, nodename);
62 return false;
63 }
64
65 /* A file named '...' is likely to be a path traversal attempt, see http://cwe.mitre.org/data/definitions/32.html */
66 if (strcmp(nodename, "...") == 0)
67 {
68 Log(LOG_LEVEL_WARNING, "Possible path traversal attempt detected in '%s/%s', skipping", path, nodename);
69 return false;
70 }
71
72 for (i = 0; SKIPFILES[i] != NULL; i++)
73 {
74 if (strcmp(nodename, SKIPFILES[i]) == 0)
75 {
76 Log(LOG_LEVEL_VERBOSE, "Filename '%s/%s' is classified as ignorable, skipping", path, nodename);
77 return false;
78 }
79 }
80
81 return true;
82 }
83
ConsiderLocalFile(const char * filename,const char * directory)84 bool ConsiderLocalFile(const char *filename, const char *directory)
85 {
86 struct stat stat;
87 if (lstat(filename, &stat) == -1)
88 {
89 return ConsiderFile(filename, directory, NULL);
90 }
91 else
92 {
93 return ConsiderFile(filename, directory, &stat);
94 }
95 }
96
ConsiderAbstractFile(const char * filename,const char * directory,const FileCopy * fc,AgentConnection * conn)97 bool ConsiderAbstractFile(const char *filename, const char *directory, const FileCopy *fc, AgentConnection *conn)
98 {
99 /* First check if the file should be avoided, e.g. ".." - before sending
100 * anything over the network*/
101
102 if (!ConsiderFile(filename, directory, NULL))
103 {
104 return false;
105 }
106
107 /* TODO this function should accept the joined path in the first place
108 * since it's joined elsewhere as well, if split needed do it here. */
109 char buf[CF_BUFSIZE];
110 int ret = snprintf(buf, sizeof(buf), "%s/%s", directory, filename);
111 if (ret < 0)
112 {
113 Log(LOG_LEVEL_ERR,
114 "Unexpected failure from snprintf (%d - %s) on '%s' "
115 "(ConsiderAbstractFile)", errno, GetErrorStr(), buf);
116 return false;
117 }
118 else if ((size_t) ret >= sizeof(buf))
119 {
120 Log(LOG_LEVEL_ERR,
121 "Filename too long! Directory '%s' filename '%s'",
122 directory, filename);
123 return false;
124 }
125
126 /* Second, send the STAT command. */
127
128 struct stat stat;
129 if (cf_lstat(buf, &stat, fc, conn) == -1)
130 {
131 return false; /* stat() failed */
132 }
133 else
134 {
135 /* Reconsider, but using stat info this time. */
136 return ConsiderFile(filename, directory, &stat);
137 }
138 }
139