Changeset 389

Show
Ignore:
Timestamp:
02/19/10 04:13:07 (2 years ago)
Author:
walter
Message:

test AA's for equality; tighten security on file imports

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dmd-1.x/src/e2ir.c

    r266 r389  
    20102010    Type *telement = t1->nextOf()->toBasetype(); 
    20112011    int rtlfunc; 
    20122012 
    20132013    ea1 = e1->toElem(irs); 
    20142014    ea1 = array_toDarray(t1, ea1); 
    20152015    ea2 = e2->toElem(irs); 
    20162016    ea2 = array_toDarray(t2, ea2); 
    20172017 
    20182018#if DMDV2 
    20192019    ep = el_params(telement->arrayOf()->getInternalTypeInfo(NULL)->toElem(irs), 
    20202020        ea2, ea1, NULL); 
    20212021    rtlfunc = RTLSYM_ARRAYEQ2; 
    20222022#else 
    20232023    ep = el_params(telement->getInternalTypeInfo(NULL)->toElem(irs), ea2, ea1, NULL); 
    20242024    rtlfunc = RTLSYM_ARRAYEQ; 
    20252025#endif 
    20262026    e = el_bin(OPcall, TYint, el_var(rtlsym[rtlfunc]), ep); 
    20272027    if (op == TOKnotequal) 
    20282028        e = el_bin(OPxor, TYint, e, el_long(TYint, 1)); 
    20292029    el_setLoc(e,loc); 
     2030    } 
     2031    else if (t1->ty == Taarray && t2->ty == Taarray) 
     2032    {   TypeAArray *taa = (TypeAArray *)t1; 
     2033    Symbol *s = taa->aaGetSymbol("Equal", 0); 
     2034    elem *ti = taa->getTypeInfo(NULL)->toElem(irs); 
     2035    elem *ea1 = e1->toElem(irs); 
     2036    elem *ea2 = e2->toElem(irs); 
     2037    // aaEqual(ti, e1, e2) 
     2038    elem *ep = el_params(ea2, ea1, ti, NULL); 
     2039    e = el_bin(OPcall, TYnptr, el_var(s), ep); 
     2040    if (op == TOKnotequal) 
     2041        e = el_bin(OPxor, TYint, e, el_long(TYint, 1)); 
     2042    el_setLoc(e,loc); 
     2043    return e; 
    20302044    } 
    20312045    else 
    20322046    e = toElemBin(irs, eop); 
    20332047    return e; 
    20342048} 
    20352049 
    20362050elem *IdentityExp::toElem(IRState *irs) 
    20372051{ 
    20382052    elem *e; 
    20392053    enum OPER eop; 
    20402054    Type *t1 = e1->type->toBasetype(); 
    20412055    Type *t2 = e2->type->toBasetype(); 
    20422056 
    20432057    switch (op) 
    20442058    { 
    20452059    case TOKidentity:   eop = OPeqeq;   break; 
    20462060    case TOKnotidentity:    eop = OPne; break; 
    20472061    default: 
    20482062        dump(0); 
    20492063        assert(0); 
  • branches/dmd-1.x/src/expression.c

    r382 r389  
    52405240{   char *name; 
    52415241    StringExp *se; 
    52425242 
    52435243#if LOGSEMANTIC 
    52445244    printf("FileExp::semantic('%s')\n", toChars()); 
    52455245#endif 
    52465246    UnaExp::semantic(sc); 
    52475247    e1 = resolveProperties(sc, e1); 
    52485248    e1 = e1->optimize(WANTvalue); 
    52495249    if (e1->op != TOKstring) 
    52505250    {   error("file name argument must be a string, not (%s)", e1->toChars()); 
    52515251    goto Lerror; 
    52525252    } 
    52535253    se = (StringExp *)e1; 
    52545254    se = se->toUTF8(sc); 
    52555255    name = (char *)se->string; 
    52565256 
    52575257    if (!global.params.fileImppath) 
    52585258    {   error("need -Jpath switch to import text file %s", name); 
    52595259    goto Lerror; 
     5260    } 
     5261 
     5262    /* Be wary of CWE-22: Improper Limitation of a Pathname to a Restricted Directory 
     5263     * ('Path Traversal') attacks. 
     5264     * http://cwe.mitre.org/data/definitions/22.html 
     5265     */ 
     5266 
     5267    /* Do harsh sanitizing by limiting the name's character set. 
     5268     */ 
     5269    for (const char *p = name; *p; p++) 
     5270    { 
     5271    if (!(isalnum(*p) || *p == '.' || *p == '_')) 
     5272    { 
     5273        error("file name characters are restricted to [a-zA-Z0-9._] not '%c'", *p); 
     5274        goto Lerror; 
     5275    } 
    52605276    } 
    52615277 
    52625278    if (name != FileName::name(name)) 
    52635279    {   error("use -Jpath switch to provide path for filename %s", name); 
    52645280    goto Lerror; 
    52655281    } 
    52665282 
    52675283    name = FileName::searchPath(global.filePath, name, 0); 
    52685284    if (!name) 
    52695285    {   error("file %s cannot be found, check -Jpath", se->toChars()); 
    52705286    goto Lerror; 
    52715287    } 
    52725288 
    52735289    if (global.params.verbose) 
    52745290    printf("file      %s\t(%s)\n", (char *)se->string, name); 
    52755291 
    52765292    {   File f(name); 
    52775293    if (f.read()) 
    52785294    {   error("cannot read file %s", f.toChars()); 
    52795295        goto Lerror; 
  • trunk/src/e2ir.c

    r368 r389  
    11 
    22// Compiler implementation of the D programming language 
    3 // Copyright (c) 1999-2009 by Digital Mars 
     3// Copyright (c) 1999-2010 by Digital Mars 
    44// All Rights Reserved 
    55// written by Walter Bright 
    66// http://www.digitalmars.com 
    77// License for redistribution is by either the Artistic License 
    88// in artistic.txt, or the GNU General Public License in gnu.txt. 
    99// See the included readme.txt for details. 
    1010 
    1111#include    <stdio.h> 
    1212#include    <string.h> 
    1313#include    <time.h> 
    1414#include    <complex.h> 
    1515 
    1616#include    "port.h" 
    1717 
    1818#include    "lexer.h" 
    1919#include    "expression.h" 
    2020#include    "mtype.h" 
    2121#include    "dsymbol.h" 
    2222#include    "declaration.h" 
    2323#include    "enum.h" 
     
    22952295    { 
    22962296    Type *telement = t1->nextOf()->toBasetype(); 
    22972297 
    22982298    elem *ea1 = e1->toElem(irs); 
    22992299    ea1 = array_toDarray(t1, ea1); 
    23002300    elem *ea2 = e2->toElem(irs); 
    23012301    ea2 = array_toDarray(t2, ea2); 
    23022302 
    23032303#if DMDV2 
    23042304    elem *ep = el_params(telement->arrayOf()->getInternalTypeInfo(NULL)->toElem(irs), 
    23052305        ea2, ea1, NULL); 
    23062306    int rtlfunc = RTLSYM_ARRAYEQ2; 
    23072307#else 
    23082308    elem *ep = el_params(telement->getInternalTypeInfo(NULL)->toElem(irs), ea2, ea1, NULL); 
    23092309    int rtlfunc = RTLSYM_ARRAYEQ; 
    23102310#endif 
    23112311    e = el_bin(OPcall, TYint, el_var(rtlsym[rtlfunc]), ep); 
    23122312    if (op == TOKnotequal) 
    23132313        e = el_bin(OPxor, TYint, e, el_long(TYint, 1)); 
    23142314    el_setLoc(e,loc); 
     2315    } 
     2316    else if (t1->ty == Taarray && t2->ty == Taarray) 
     2317    {   TypeAArray *taa = (TypeAArray *)t1; 
     2318    Symbol *s = taa->aaGetSymbol("Equal", 0); 
     2319    elem *ti = taa->getTypeInfo(NULL)->toElem(irs); 
     2320    elem *ea1 = e1->toElem(irs); 
     2321    elem *ea2 = e2->toElem(irs); 
     2322    // aaEqual(ti, e1, e2) 
     2323    elem *ep = el_params(ea2, ea1, ti, NULL); 
     2324    e = el_bin(OPcall, TYnptr, el_var(s), ep); 
     2325    if (op == TOKnotequal) 
     2326        e = el_bin(OPxor, TYint, e, el_long(TYint, 1)); 
     2327    el_setLoc(e,loc); 
     2328    return e; 
    23152329    } 
    23162330    else 
    23172331    e = toElemBin(irs, eop); 
    23182332    return e; 
    23192333} 
    23202334 
    23212335elem *IdentityExp::toElem(IRState *irs) 
    23222336{ 
    23232337    elem *e; 
    23242338    enum OPER eop; 
    23252339    Type *t1 = e1->type->toBasetype(); 
    23262340    Type *t2 = e2->type->toBasetype(); 
    23272341 
    23282342    switch (op) 
    23292343    { 
    23302344    case TOKidentity:   eop = OPeqeq;   break; 
    23312345    case TOKnotidentity:    eop = OPne; break; 
    23322346    default: 
    23332347        dump(0); 
    23342348        assert(0); 
  • trunk/src/expression.c

    r382 r389  
    55845584 
    55855585#if LOGSEMANTIC 
    55865586    printf("FileExp::semantic('%s')\n", toChars()); 
    55875587#endif 
    55885588    UnaExp::semantic(sc); 
    55895589    e1 = resolveProperties(sc, e1); 
    55905590    e1 = e1->optimize(WANTvalue); 
    55915591    if (e1->op != TOKstring) 
    55925592    {   error("file name argument must be a string, not (%s)", e1->toChars()); 
    55935593    goto Lerror; 
    55945594    } 
    55955595    se = (StringExp *)e1; 
    55965596    se = se->toUTF8(sc); 
    55975597    name = (char *)se->string; 
    55985598 
    55995599    if (!global.params.fileImppath) 
    56005600    {   error("need -Jpath switch to import text file %s", name); 
    56015601    goto Lerror; 
    56025602    } 
    56035603 
     5604    /* Be wary of CWE-22: Improper Limitation of a Pathname to a Restricted Directory 
     5605     * ('Path Traversal') attacks. 
     5606     * http://cwe.mitre.org/data/definitions/22.html 
     5607     */ 
     5608 
     5609    /* Do harsh sanitizing by limiting the name's character set. 
     5610     */ 
     5611    for (const char *p = name; *p; p++) 
     5612    { 
     5613    if (!(isalnum(*p) || *p == '.' || *p == '_')) 
     5614    { 
     5615        error("file name characters are restricted to [a-zA-Z0-9._] not '%c'", *p); 
     5616        goto Lerror; 
     5617    } 
     5618    } 
     5619 
    56045620    if (name != FileName::name(name)) 
    56055621    {   error("use -Jpath switch to provide path for filename %s", name); 
    56065622    goto Lerror; 
    56075623    } 
    56085624 
    56095625    name = FileName::searchPath(global.filePath, name, 0); 
    56105626    if (!name) 
    56115627    {   error("file %s cannot be found, check -Jpath", se->toChars()); 
    56125628    goto Lerror; 
    56135629    } 
    56145630 
    56155631    if (global.params.verbose) 
    56165632    printf("file      %s\t(%s)\n", (char *)se->string, name); 
    56175633 
    56185634    {   File f(name); 
    56195635    if (f.read()) 
    56205636    {   error("cannot read file %s", f.toChars()); 
    56215637        goto Lerror; 
    56225638    } 
    56235639    else