1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* sqUnixSecurity.c -- directory operations for Unix
*
* Author: Bert Freudenberg (heavily based on Andreas Raab's sqWin32Security.c)
*
* Last edited: 2009-09-02 15:11:46 by piumarta on ubuntu.piumarta.com
*
* Note: According to Ian Piumarta, the Unix VM is inherently insecure since
* pluggable primitives can access all of libc! It would need
* some linker magic to hide these from dlsym().
*
* A workaround would be to disallow lookups via dlsym() when
* fileaccess is disallowed - internal plugins should still work ...
*/
#include "sq.h"
#include "SecurityPlugin.h"
#include <sys/param.h>
static char secureUserDirectory[MAXPATHLEN]; /* default: imagepath/secure */
static char untrustedUserDirectory[MAXPATHLEN]; /* default: imagepath/My Squeak */
static int untrustedUserDirectoryLen;
static char resourceDirectory[MAXPATHLEN]; /* default: imagepath */
static int resourceDirectoryLen;
static char* fromSqueak(char* string, int len)
{
static char buf[MAXPATHLEN];
strncpy(buf, string, len);
buf[len]= '\0';
return buf;
}
/* file security ***********************************************************/
static sqInt allowFileAccess= 1; /* full access to files */
static int isAccessiblePathName(char *pathName, int writeFlag)
{
char realPathName[MAXPATHLEN];
int realPathLen;
realPathName == realpath(pathName, realPathName);
realPathLen= strlen(realPathName);
if (realPathLen >= untrustedUserDirectoryLen
&& 0 == strncmp(realPathName, untrustedUserDirectory, untrustedUserDirectoryLen))
return 1;
if (writeFlag)
return 0;
return (realPathLen >= resourceDirectoryLen
&& 0 == strncmp(realPathName, resourceDirectory, resourceDirectoryLen));
}
static int isAccessibleFileName(char *fileName, int writeFlag)
{
char pathName[MAXPATHLEN];
int pathLen= strrchr(fileName, '/') - fileName;
strncpy(pathName, fileName, pathLen);
pathName[pathLen]= '\0';
return isAccessiblePathName(pathName, writeFlag);
}
/* directory access */
sqInt ioCanCreatePathOfSize(char* pathString, sqInt pathStringLength)
{
if (allowFileAccess) return 1;
return isAccessiblePathName(fromSqueak(pathString, pathStringLength), 1);
}
sqInt ioCanListPathOfSize(char* pathString, sqInt pathStringLength)
{
if (allowFileAccess) return 1;
return isAccessiblePathName(fromSqueak(pathString, pathStringLength), 0);
}
sqInt ioCanDeletePathOfSize(char* pathString, sqInt pathStringLength)
{
if (allowFileAccess) return 1;
return isAccessiblePathName(fromSqueak(pathString, pathStringLength), 1);
}
/* file access */
sqInt ioCanOpenFileOfSizeWritable(char* pathString, sqInt pathStringLength, sqInt writeFlag)
{
if (allowFileAccess) return 1;
return isAccessibleFileName(fromSqueak(pathString, pathStringLength), writeFlag);
}
sqInt ioCanOpenAsyncFileOfSizeWritable(char* pathString, sqInt pathStringLength, sqInt writeFlag)
{
return ioCanOpenFileOfSizeWritable(pathString, pathStringLength, writeFlag);
}
sqInt ioCanDeleteFileOfSize(char* pathString, sqInt pathStringLength)
{
if (allowFileAccess) return 1;
return isAccessibleFileName(fromSqueak(pathString, pathStringLength), 1);
}
sqInt ioCanRenameFileOfSize(char* pathString, sqInt pathStringLength)
{
if (allowFileAccess) return 1;
return isAccessibleFileName(fromSqueak(pathString, pathStringLength), 1);
}
sqInt ioCanGetFileTypeOfSize(char* pathString, sqInt pathStringLength)
{
return 1; /* we don't have file types */
}
sqInt ioCanSetFileTypeOfSize(char* pathString, sqInt pathStringLength)
{
return 1; /* we don't have file types */
}
/* disabling/querying */
sqInt ioDisableFileAccess(void)
{
allowFileAccess= 0;
return 1;
}
sqInt ioHasFileAccess(void)
{
return allowFileAccess;
}
/* image security **********************************************************/
static sqInt allowImageWrite= 1; /* allow writing the image */
sqInt ioCanRenameImage(void)
{
return allowImageWrite; /* only when we're allowed to save the image */
}
sqInt ioCanWriteImage(void)
{
return allowImageWrite;
}
sqInt ioDisableImageWrite(void)
{
allowImageWrite= 0;
return 1;
}
/* socket security - for now it's all or nothing ***************************/
static sqInt allowSocketAccess= 1; /* allow access to sockets */
sqInt ioCanCreateSocketOfType(sqInt netType, sqInt socketType)
{
return allowSocketAccess;
}
sqInt ioCanConnectToPort(sqInt netAddr, sqInt port)
{
return allowSocketAccess;
}
sqInt ioCanListenOnPort(sqInt s, sqInt port)
{
return allowSocketAccess;
}
sqInt ioDisableSocketAccess()
{
allowSocketAccess= 0;
return 1;
}
sqInt ioHasSocketAccess()
{
return allowSocketAccess;
}
/* SecurityPlugin primitive support ****************************************/
char *ioGetSecureUserDirectory(void)
{
if (secureUserDirectory[0] == '\0')
return (char *)success(false);
return secureUserDirectory;
}
char *ioGetUntrustedUserDirectory(void)
{
return untrustedUserDirectory;
}
/* note: following is called from VM directly, not from plugin */
sqInt ioInitSecurity(void)
{
int imagePathLen= strrchr(imageName, '/') - imageName;
char *directory= 0;
/* establish the secure user directory */
directory= getenv("SQUEAK_SECUREDIR");
if (0 == directory)
{
strncpy(secureUserDirectory, imageName, imagePathLen);
strcpy(secureUserDirectory + imagePathLen, "/secure");
}
else
{
int lastChar= strlen(directory);
/* path is not allowed to end with "/" */
if ('/' == directory[lastChar - 1])
directory[lastChar - 1]= '\0';
strcpy(secureUserDirectory, directory);
}
/* establish untrusted user directory */
directory= getenv("SQUEAK_USERDIR");
if (0 == directory)
{
strncpy(untrustedUserDirectory, imageName, imagePathLen);
strcpy(untrustedUserDirectory + imagePathLen, "/My Squeak");
}
else
{
int lastChar= strlen(directory);
/* path is not allowed to end with "/" */
if ('/' == directory[lastChar - 1])
directory[lastChar - 1]= '\0';
strcpy(untrustedUserDirectory, directory);
}
untrustedUserDirectoryLen= strlen(untrustedUserDirectory);
/* establish resource directory */
directory= getenv("SQUEAK_RESOURCEDIR");
if (0 == directory)
{
strncpy(resourceDirectory, imageName, imagePathLen);
}
else
{
int lastChar= strlen(directory);
/* path is not allowed to end with "/" */
if ('/' == directory[lastChar - 1])
directory[lastChar - 1]= '\0';
strcpy(resourceDirectory, directory);
}
resourceDirectoryLen= strlen(resourceDirectory);
return 1;
}
|