3 kHTTPd -- the next generation
5 Permissions/Security functions
9 /****************************************************************
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ****************************************************************/
27 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/net.h>
32 #include <linux/sched.h>
33 #include <linux/skbuff.h>
34 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
42 #include <asm/atomic.h>
43 #include <asm/semaphore.h>
44 #include <asm/processor.h>
45 #include <asm/uaccess.h>
47 #include <linux/file.h>
51 #include "prototypes.h"
55 The basic security function answers "Userspace" when any one of the following
58 1) The filename contains a "?" (this is before % decoding, all others are
60 2) The filename doesn't start with a "/"
61 3) The file does not exist
62 4) The file does not have enough permissions
63 (sysctl-configurable, default = worldreadble)
64 5) The file has any of the "forbidden" permissions
65 (sysctl-configurable, default = execute, directory and sticky)
66 6) The filename contains a string as defined in the "Dynamic" list.
73 static void DecodeHexChars(char *URL);
74 static struct DynamicString *DynamicList=NULL;
80 The function "OpenFileForSecurity" returns either the "struct file" pointer
81 of the file, or NULL. NULL means "let userspace handle it".
84 struct file *OpenFileForSecurity(char *Filename)
86 struct file *filp = NULL;
87 struct DynamicString *List;
90 EnterFunction("OpenFileForSecurity");
94 if (strlen(Filename)>=256 )
95 goto out_error; /* Sanity check */
97 /* Rule no. 1 -- No "?" characters */
99 if (strchr(Filename,'?')!=NULL)
102 /* Intermediate step: decode all %hex sequences */
104 DecodeHexChars(Filename);
106 /* Rule no. 2 -- Must start with a "/" */
108 if (Filename[0]!='/')
112 /* Rule no. 3 -- Does the file exist ? */
114 filp = filp_open(Filename, O_RDONLY, 0);
120 permission = filp->f_dentry->d_inode->i_mode;
122 /* Rule no. 4 : must have enough permissions */
124 if ((permission & sysctl_khttpd_permreq)==0)
127 /* Rule no. 5 : cannot have "forbidden" permission */
129 if ((permission & sysctl_khttpd_permforbid)!=0)
132 /* Rule no. 6 : No string in DynamicList can be a
133 substring of the filename */
138 if (strstr(Filename,List->value)!=NULL)
145 LeaveFunction("OpenFileForSecurity - success");
153 LeaveFunction("OpenFileForSecurity - fail");
159 DecodeHexChars does the actual %HEX decoding, in place.
160 In place is possible because strings only get shorter by this.
163 static void DecodeHexChars(char *URL)
168 EnterFunction("DecodeHexChars");
170 Source = strchr(URL,'%');
184 if (val>'Z') val-=0x20;
194 if (val2>'Z') val2-=0x20;
198 if (val2>15) val2=15;
201 } else *Dest = *Source;
207 LeaveFunction("DecodeHexChars");
211 void AddDynamicString(const char *String)
213 struct DynamicString *Temp;
215 EnterFunction("AddDynamicString");
217 Temp = (struct DynamicString*)kmalloc(sizeof(struct DynamicString),(int)GFP_KERNEL);
222 memset(Temp->value,0,sizeof(Temp->value));
223 strncpy(Temp->value,String,sizeof(Temp->value)-1);
225 Temp->Next = DynamicList;
228 LeaveFunction("AddDynamicString");
231 void GetSecureString(char *String)
233 struct DynamicString *Temp;
236 EnterFunction("GetSecureString");
240 memset(String,0,255);
242 strncpy(String,"Dynamic strings are : -",255);
246 max=253 - strlen(String) - strlen(Temp->value);
247 strncat(String,Temp->value,max);
248 max=253 - strlen(String) - 3;
249 strncat(String,"- -",max);
253 LeaveFunction("GetSecureString");