OPENAFS-SA-2016-002 AFSStoreStatus information leak
[openafs.git] / src / afs / VNOPS / afs_vnop_symlink.c
index c09c35f..f608b2b 100644 (file)
@@ -37,7 +37,6 @@ extern afs_rwlock_t afs_xcbhash;
  * is just a performance hit.
  */
 
-#ifdef AFS_DISCON_ENV
 static int
 afs_DisconCreateSymlink(struct vcache *avc, char *aname, 
                        struct vrequest *areq) {
@@ -48,7 +47,7 @@ afs_DisconCreateSymlink(struct vcache *avc, char *aname,
     tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
     if (!tdc) {
        /* printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n"); */
-       return ENOENT;
+       return ENETDOWN;
     }
 
     len = strlen(aname);
@@ -63,15 +62,14 @@ afs_DisconCreateSymlink(struct vcache *avc, char *aname,
     ReleaseWriteLock(&tdc->lock);
     return 0;
 }
-#endif
 
 /* don't set CDirty in here because RPC is called synchronously */
 int 
 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs, 
-           char *atargetName, afs_ucred_t *acred)
+           char *atargetName, struct vcache **tvcp, afs_ucred_t *acred)
 {
     afs_uint32 now = 0;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
     afs_int32 code = 0;
     struct afs_conn *tc;
     struct VenusFid newFid;
@@ -81,11 +79,12 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
     struct server *hostp = 0;
     struct vcache *tvc;
     struct AFSStoreStatus InStatus;
-    struct AFSFetchStatus OutFidStatus, OutDirStatus;
+    struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
     struct AFSCallBack CallBack;
     struct AFSVolSync tsync;
     struct volume *volp = 0;
     struct afs_fakestat_state fakestate;
+    struct rx_connection *rxconn;
     XSTATS_DECLS;
     OSI_VC_CONVERT(adp);
 
@@ -93,14 +92,18 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
     afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
               ICL_TYPE_STRING, aname);
 
-    if ((code = afs_InitReq(&treq, acred)))
+    OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
+    OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
+    memset(&InStatus, 0, sizeof(InStatus));
+
+    if ((code = afs_CreateReq(&treq, acred)))
        goto done2;
 
     afs_InitFakeStat(&fakestate);
 
     AFS_DISCON_LOCK();
     
-    code = afs_EvalFakeStat(&adp, &fakestate, &treq);
+    code = afs_EvalFakeStat(&adp, &fakestate, treq);
     if (code)
        goto done;
 
@@ -118,9 +121,9 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
        goto done;
     }
 
-    code = afs_VerifyVCache(adp, &treq);
+    code = afs_VerifyVCache(adp, treq);
     if (code) {
-       code = afs_CheckCode(code, &treq, 30);
+       code = afs_CheckCode(code, treq, 30);
        goto done;
     }
 
@@ -148,7 +151,7 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
        InStatus.UnixModeBits = 0755;
        alen++;                 /* add in the null */
     }
-    tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
+    tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);
     volp = afs_FindVolume(&adp->f.fid, READ_LOCK);     /*parent is also in same vol */
     ObtainWriteLock(&adp->lock, 156);
     if (tdc)
@@ -158,42 +161,40 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
      * the copy will be invalidated */
     if (!AFS_IS_DISCON_RW) {
        do {
-           tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
+           tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
            if (tc) {
-               hostp = tc->srvr->server;
+               hostp = tc->parent->srvr->server;
                XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
                if (adp->f.states & CForeign) {
                    now = osi_Time();
                    RX_AFS_GUNLOCK();
                    code = 
-                       RXAFS_DFSSymlink(tc->id, 
+                       RXAFS_DFSSymlink(rxconn,
                                         (struct AFSFid *)&adp->f.fid.Fid,
                                         aname, atargetName, &InStatus,
                                         (struct AFSFid *)&newFid.Fid,
-                                        &OutFidStatus, &OutDirStatus, 
+                                        OutFidStatus, OutDirStatus,
                                         &CallBack, &tsync);
                    RX_AFS_GLOCK();
                } else {
                    RX_AFS_GUNLOCK();
                    code =
-                       RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
+                       RXAFS_Symlink(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
                                      aname, atargetName, &InStatus,
                                      (struct AFSFid *)&newFid.Fid, 
-                                     &OutFidStatus, &OutDirStatus, &tsync);
+                                     OutFidStatus, OutDirStatus, &tsync);
                    RX_AFS_GLOCK();
                }
                XSTATS_END_TIME;
            } else
                code = -1;
        } while (afs_Analyze
-                   (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
+                   (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_SYMLINK,
                     SHARED_LOCK, NULL));
     } else {
-#ifdef AFS_DISCON_ENV
        newFid.Cell = adp->f.fid.Cell;
        newFid.Fid.Volume = adp->f.fid.Fid.Volume;
        afs_GenFakeFid(&newFid, VREG, 0);
-#endif
     }
 
     ObtainWriteLock(&afs_xvcache, 40);
@@ -214,7 +215,7 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
        goto done;
     }
     /* otherwise, we should see if we can make the change to the dir locally */
-    if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
+    if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
        /* we can do it locally */
        ObtainWriteLock(&afs_xdcache, 293);
        /* If the following fails because the name has been created in the meantime, the
@@ -263,10 +264,9 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
     ReleaseWriteLock(&afs_xcbhash);
 
     if (AFS_IS_DISCON_RW) {
-#ifdef AFS_DISCON_ENV
        attrs->va_mode = InStatus.UnixModeBits;
-       afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VLNK);
-       code = afs_DisconCreateSymlink(tvc, atargetName, &treq);
+       afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VLNK);
+       code = afs_DisconCreateSymlink(tvc, atargetName, treq);
        if (code) {
            /* XXX - When this goes wrong, we need to tidy up the changes we made to
             * the parent, and get rid of the vcache we just created */
@@ -276,38 +276,44 @@ afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
            goto done;
        }
        afs_DisconAddDirty(tvc, VDisconCreate, 0);
-#endif
     } else {
-       afs_ProcessFS(tvc, &OutFidStatus, &treq);
+       afs_ProcessFS(tvc, OutFidStatus, treq);
     }
 
     if (!tvc->linkData) {
-       tvc->linkData = (char *)afs_osi_Alloc(alen);
+       tvc->linkData = afs_osi_Alloc(alen);
+       osi_Assert(tvc->linkData != NULL);
        strncpy(tvc->linkData, atargetName, alen - 1);
        tvc->linkData[alen - 1] = 0;
     }
     ReleaseWriteLock(&tvc->lock);
     ReleaseWriteLock(&afs_xvcache);
-    afs_PutVCache(tvc);
+    if (tvcp)
+       *tvcp = tvc;
+    else
+       afs_PutVCache(tvc);
     code = 0;
   done:
     afs_PutFakeStat(&fakestate);
     if (volp)
        afs_PutVolume(volp, READ_LOCK);
     AFS_DISCON_UNLOCK();
-    code = afs_CheckCode(code, &treq, 31);
+    code = afs_CheckCode(code, treq, 31);
+    afs_DestroyReq(treq);
   done2:
+    osi_FreeSmallSpace(OutFidStatus);
+    osi_FreeSmallSpace(OutDirStatus);
     return code;
 }
 
 int
-afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
+afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
 {
-    register struct dcache *tdc;
-    register char *tp, *rbuf;
+    struct dcache *tdc;
+    char *tp, *rbuf;
     afs_size_t offset, len;
     afs_int32 tlen, alen;
-    register afs_int32 code;
+    afs_int32 code;
 
     AFS_STATCNT(afs_MemHandleLink);
     /* two different formats, one for links protected 644, have a "." at
@@ -328,7 +334,7 @@ afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
            alen = len + 1;     /* regular link */
        else
            alen = len;         /* mt point */
-       rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
+       rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
        ObtainReadLock(&tdc->lock);
        addr = afs_MemCacheOpen(&tdc->f.inode);
        tlen = len;
@@ -339,6 +345,7 @@ afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
        rbuf[alen - 1] = 0;
        alen = strlen(rbuf) + 1;
        tp = afs_osi_Alloc(alen);       /* make room for terminating null */
+       osi_Assert(tp != NULL);
        memcpy(tp, rbuf, alen);
        osi_FreeLargeSpace(rbuf);
        if (code != len) {
@@ -351,14 +358,14 @@ afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
 }
 
 int
-afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
+afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
 {
-    register struct dcache *tdc;
-    register char *tp, *rbuf;
+    struct dcache *tdc;
+    char *tp, *rbuf;
     void *tfile;
     afs_size_t offset, len;
     afs_int32 tlen, alen;
-    register afs_int32 code;
+    afs_int32 code;
 
     /* two different formats, one for links protected 644, have a "." at the
      * end of the file name, which we turn into a null.  Others, protected
@@ -384,10 +391,16 @@ afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
            alen = len + 1;     /* regular link */
        else
            alen = len;         /* mt point */
-       rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
+       rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
        tlen = len;
        ObtainReadLock(&tdc->lock);
        tfile = osi_UFSOpen(&tdc->f.inode);
+       if (!tfile) {
+           ReleaseReadLock(&tdc->lock);
+           afs_PutDCache(tdc);
+           osi_FreeLargeSpace(rbuf);
+           return EIO;
+       }
        code = afs_osi_Read(tfile, -1, rbuf, tlen);
        osi_UFSClose(tfile);
        ReleaseReadLock(&tdc->lock);
@@ -395,6 +408,7 @@ afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
        rbuf[alen - 1] = '\0';
        alen = strlen(rbuf) + 1;
        tp = afs_osi_Alloc(alen);       /* make room for terminating null */
+       osi_Assert(tp != NULL);
        memcpy(tp, rbuf, alen);
        osi_FreeLargeSpace(rbuf);
        if (code != tlen) {
@@ -409,24 +423,24 @@ afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
 int
 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
 {
-    register afs_int32 code;
-    struct vrequest treq;
-    register char *tp;
+    afs_int32 code;
+    struct vrequest *treq = NULL;
+    char *tp;
     struct afs_fakestat_state fakestat;
     OSI_VC_CONVERT(avc);
 
     AFS_STATCNT(afs_readlink);
     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
-    if ((code = afs_InitReq(&treq, acred)))
+    if ((code = afs_CreateReq(&treq, acred)))
        return code;
     afs_InitFakeStat(&fakestat);
 
     AFS_DISCON_LOCK();
     
-    code = afs_EvalFakeStat(&avc, &fakestat, &treq);
+    code = afs_EvalFakeStat(&avc, &fakestat, treq);
     if (code)
        goto done;
-    code = afs_VerifyVCache(avc, &treq);
+    code = afs_VerifyVCache(avc, treq);
     if (code)
        goto done;
     if (vType(avc) != VLNK) {
@@ -434,7 +448,7 @@ afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
        goto done;
     }
     ObtainWriteLock(&avc->lock, 158);
-    code = afs_HandleLink(avc, &treq);
+    code = afs_HandleLink(avc, treq);
     /* finally uiomove it to user-land */
     if (code == 0) {
        tp = avc->linkData;
@@ -448,6 +462,7 @@ afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
   done:
     afs_PutFakeStat(&fakestat);
     AFS_DISCON_UNLOCK();
-    code = afs_CheckCode(code, &treq, 32);
+    code = afs_CheckCode(code, treq, 32);
+    afs_DestroyReq(treq);
     return code;
 }