krb5 commit [krb5-1.13]: Work around replay cache creation race

Tom Yu tlyu at mit.edu
Thu Sep 18 16:46:09 EDT 2014


https://github.com/krb5/krb5/commit/99e08376c14240e2141c6fa9289fafab8245c754
commit 99e08376c14240e2141c6fa9289fafab8245c754
Author: Greg Hudson <ghudson at mit.edu>
Date:   Wed Sep 17 10:45:28 2014 -0400

    Work around replay cache creation race
    
    If two processes try to initialize the same replay cache at the same
    time, krb5_rc_io_creat can race between unlink and open, leading to a
    KRB5_RC_IO_PERM error.  When this happens, make the losing process
    retry so that it can continue.
    
    This does not solve the replay cache creation race, nor is that the
    only replay cache race issue.  It simply prevents the race from
    causing a spurious failure.
    
    (cherry picked from commit c61e8c0c6ad5fda8d23dd896c4aed0ac5b470020)
    
    ticket: 3498
    version_fixed: 1.13
    status: resolved

 src/lib/krb5/rcache/rc_io.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/lib/krb5/rcache/rc_io.c b/src/lib/krb5/rcache/rc_io.c
index 7e3b7e9..b9859fe 100644
--- a/src/lib/krb5/rcache/rc_io.c
+++ b/src/lib/krb5/rcache/rc_io.c
@@ -158,7 +158,7 @@ krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn)
 {
     krb5_int16 rc_vno = htons(KRB5_RC_VNO);
     krb5_error_code retval = 0;
-    int do_not_unlink = 0;
+    int flags, do_not_unlink = 0;
     char *dir;
     size_t dirlen;
 
@@ -166,9 +166,13 @@ krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn)
     if (fn && *fn) {
         if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, *fn) < 0)
             return KRB5_RC_IO_MALLOC;
-        unlink(d->fn);
-        d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL |
-                               O_BINARY, 0600);
+        d->fd = -1;
+        do {
+            if (unlink(d->fn) == -1 && errno != ENOENT)
+                break;
+            flags = O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | O_BINARY;
+            d->fd = THREEPARAMOPEN(d->fn, flags, 0600);
+        } while (d->fd == -1 && errno == EEXIST);
     } else {
         retval = krb5_rc_io_mkstemp(context, d, dir);
         if (retval)


More information about the cvs-krb5 mailing list