summarylogtreecommitdiffstats
path: root/cve-security-fix.diff
blob: 109ad1aab513977540c6e34dfb40876b696897b4 (plain)
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
Description: Patch made to re-apply NMU changes regarding a security fix, take a look at bug #530255 on Debian BTS.
Author: Andrea Veri <and@debian.org>

diff -urNad ctorrent-1.3.4+dnh3.3.2~/bencode.cpp ctorrent-1.3.4+dnh3.3.2/bencode.cpp
--- ctorrent-1.3.4+dnh3.3.2~/bencode.cpp	2008-06-15 02:00:19.000000000 +0200
+++ ctorrent-1.3.4+dnh3.3.2/bencode.cpp	2009-07-12 23:53:50.000000000 +0200
@@ -233,22 +233,28 @@
   return bencode_end_dict_list(fp);
 }
 
-size_t decode_list2path(const char *b, size_t n, char *pathname)
+size_t decode_list2path(const char *b, size_t n, char *pathname, size_t maxlen)
 {
   const char *pb = b;
-  const char *s = (char *) 0;
-  size_t r,q;
+  const char *s = (char *)0;
+  const char *endmax = pathname + maxlen - 1;
+  size_t r, q;
 
   if( 'l' != *pb ) return 0;
   pb++;
   n--;
   if( !n ) return 0;
-  for(; n;){
-    if(!(r = buf_str(pb, n, &s, &q)) ) return 0;
+  while( n && pathname < endmax ){
+   if( !(r = buf_str(pb, n, &s, &q)) ) return 0;
+   if( q >= maxlen ) return 0;
     memcpy(pathname, s, q);
     pathname += q;
-    pb += r; n -= r; 
-    if( 'e' != *pb ){*pathname = PATH_SP, pathname++;} else break;
+    maxlen -= q;
+    pb += r;
+    n -= r;
+    if( 'e' == *pb ) break;
+    if( pathname >= endmax ) return 0;
+    *pathname++ = PATH_SP;
   }
   *pathname = '\0';
   return (pb - b + 1);
diff -urNad ctorrent-1.3.4+dnh3.3.2~/bencode.h ctorrent-1.3.4+dnh3.3.2/bencode.h
--- ctorrent-1.3.4+dnh3.3.2~/bencode.h	2008-06-15 02:00:19.000000000 +0200
+++ ctorrent-1.3.4+dnh3.3.2/bencode.h	2009-07-13 00:04:29.000000000 +0200
@@ -25,7 +25,7 @@
 size_t decode_list(const char *b,size_t len,const char *keylist);
 size_t decode_rev(const char *b,size_t len,const char *keylist);
 size_t decode_query(const char *b,size_t len,const char *keylist,const char **ps,size_t *pi,int64_t *pl,int method);
-size_t decode_list2path(const char *b, size_t n, char *pathname);
+size_t decode_list2path(const char *b, size_t n, char *pathname, size_t maxlen);
 size_t bencode_buf(const char *str,size_t len,FILE *fp);
 size_t bencode_str(const char *str, FILE *fp);
 size_t bencode_int(const uint64_t integer, FILE *fp);
diff -urNad ctorrent-1.3.4+dnh3.3.2~/btfiles.cpp ctorrent-1.3.4+dnh3.3.2/btfiles.cpp
--- ctorrent-1.3.4+dnh3.3.2~/btfiles.cpp	2008-06-15 02:00:19.000000000 +0200
+++ ctorrent-1.3.4+dnh3.3.2/btfiles.cpp	2009-07-13 00:02:32.000000000 +0200
@@ -353,6 +353,7 @@
     }else{
       CONSOLE.Warning(1, "error, \"%s\" is not a directory or regular file.",
         fn);
+      errno = EINVAL;
       closedir(dp);
       return -1;
     }
@@ -444,12 +445,13 @@
   }else{
     CONSOLE.Warning(1, "error, \"%s\" is not a directory or regular file.",
       pathname);
+    errno = EINVAL;
     return -1;
   }
   return 0;
 }
 
-int btFiles::BuildFromMI(const char *metabuf, const size_t metabuf_len, const char *saveas)
+int btFiles::BuildFromMI(const char *metabuf, const size_t metabuf_len, const char *saveas, bool exam_only)
 {
   char path[MAXPATHLEN];
   const char *s, *p;
@@ -464,6 +466,13 @@
   memcpy(path, s, q);
   path[q] = '\0';
 
+  if( !exam_only &&
+      (PATH_SP == path[0] || '/' == path[0] || 0==strncmp("..", path, 2)) ){
+    CONSOLE.Warning(1, "error, unsafe path \"%s\" in torrent data", path);
+    errno = EINVAL;
+    return -1;
+  }
+
   r = decode_query(metabuf, metabuf_len, "info|files", (const char**)0, &q,
                    (int64_t*)0, QUERY_POS);
 
@@ -529,7 +538,20 @@
       r = decode_query(p, dl, "path", (const char **)0, &n, (int64_t*)0,
                        QUERY_POS);
       if( !r ) return -1;
-      if(!decode_list2path(p + r, n, path)) return -1;
+      if(!decode_list2path(p + r, n, path, sizeof(path))){
+        CONSOLE.Warning(1, "error, invalid path in torrent data for file %lu",
+          (unsigned long)m_nfiles);
+        delete pbf;
+        errno = EINVAL;
+        return -1;
+      }
+      if( !exam_only && (PATH_SP == path[0] || '/' == path[0] || 0==strncmp("..", path, 2)) ){
+        CONSOLE.Warning(1, "error, unsafe path \"%s\" in torrent data for file %lu",
+          path, (unsigned long)m_nfiles);
+        delete pbf; 
+        errno = EINVAL;
+        return -1;
+      }
 
       int f_conv;
       char *tmpfn = new char[strlen(path)*2+5];
@@ -632,22 +654,26 @@
         if( !_btf_creat_by_path(fn,pbt->bf_length)){
           CONSOLE.Warning(1, "error, create file \"%s\" failed:  %s", fn,
             strerror(errno));
+          errno = EINVAL;
           return -1;
         }
       }else{
         CONSOLE.Warning(1, "error, couldn't create file \"%s\":  %s", fn,
           strerror(errno));
+        errno = EINVAL;
         return -1;
       }
     }else{
       if( !check_exist) check_exist = 1;
       if( !(S_IFREG & sb.st_mode) ){
         CONSOLE.Warning(1, "error, file \"%s\" is not a regular file.", fn);
+        errno = EINVAL;
         return -1;
       }
       if(sb.st_size != pbt->bf_length){
         CONSOLE.Warning(1,"error, file \"%s\" size doesn't match; must be %llu",
                 fn, (unsigned long long)(pbt->bf_length));
+        errno = EINVAL;
         return -1;
       }
     }
@@ -656,6 +682,7 @@
   m_file = new BTFILE *[m_nfiles];
   if( !m_file ){
     CONSOLE.Warning(1, "error, failed to allocate memory for files list");
+    errno = ENOMEM;
     return -1;
   }
   for( pbt = m_btfhead; pbt; pbt = pbt->bf_next ){
@@ -694,6 +721,31 @@
 size_t btFiles::FillMetaInfo(FILE* fp)
 {
   BTFILE *p;
+  const char *refname, *s;
+  char path[MAXPATHLEN];
+
+  refname = m_directory ? m_directory : m_btfhead->bf_filename;
+  while( (s = strchr(refname, PATH_SP)) && *(s + 1) ){
+    refname = s + 1;
+  }
+  if( m_directory && '.' == *refname ){
+    char dir[MAXPATHLEN];
+    if( getcwd(dir, sizeof(dir)) && 0==chdir(m_directory) ){
+      if( getcwd(path, sizeof(path)) ){
+        refname = path;
+        while( (s = strchr(refname, PATH_SP)) && *(s + 1) ){
+          refname = s + 1;
+        }
+      }
+      chdir(dir);
+    }
+  }
+  if( '/' == *refname || '\0' == *refname || '.' == *refname ){
+    CONSOLE.Warning(1, "error, inappropriate file or directory name \"%s\"",
+      m_directory ? m_directory : m_btfhead->bf_filename);
+    errno = EINVAL;
+    return 0;
+  }
   if( m_directory ){
     // multi files
     if( bencode_str("files", fp) != 1 ) return 0;
@@ -715,16 +767,16 @@
     if(bencode_end_dict_list(fp) != 1 ) return 0;
     
     if(bencode_str("name", fp) != 1) return 0;
-    return bencode_str(m_directory, fp);
+    return bencode_str(refname, fp);
     
   }else{
     if( bencode_str("length", fp) != 1 ) return 0;
     if( bencode_int(m_btfhead->bf_length, fp) != 1) return 0;
     
     if( bencode_str("name", fp) != 1 ) return 0;
-    return bencode_str(m_btfhead->bf_filename, fp);
+    return bencode_str(refname, fp);
   }
-  return 1;
+  return 0;
 }
 
 
diff -urNad ctorrent-1.3.4+dnh3.3.2~/btfiles.h ctorrent-1.3.4+dnh3.3.2/btfiles.h
--- ctorrent-1.3.4+dnh3.3.2~/btfiles.h	2008-06-15 02:00:19.000000000 +0200
+++ ctorrent-1.3.4+dnh3.3.2/btfiles.h	2009-07-13 00:04:55.000000000 +0200
@@ -61,7 +61,7 @@
   
   int BuildFromFS(const char *pathname);
   int BuildFromMI(const char *metabuf, const size_t metabuf_len,
-                  const char *saveas);
+                  const char *saveas, bool exam_only);
 
   char *GetDataName() const;
   uint64_t GetTotalLength() const { return m_total_files_length; }