Skip to content

Commit e3668dd

Browse files
committed
Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
* 'for-linus' of git://oss.sgi.com/xfs/xfs: xfs: remove block number from inode lookup code xfs: rename XFS_IGET_BULKSTAT to XFS_IGET_UNTRUSTED xfs: validate untrusted inode numbers during lookup xfs: always use iget in bulkstat xfs: prevent swapext from operating on write-only files
2 parents 744c557 + 7b6259e commit e3668dd

17 files changed

Lines changed: 172 additions & 388 deletions

‎fs/xfs/linux-2.6/xfs_export.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,12 @@ xfs_nfs_get_inode(
128128
return ERR_PTR(-ESTALE);
129129

130130
/*
131-
* The XFS_IGET_BULKSTAT means that an invalid inode number is just
132-
* fine and not an indication of a corrupted filesystem. Because
133-
* clients can send any kind of invalid file handle, e.g. after
134-
* a restore on the server we have to deal with this case gracefully.
131+
* The XFS_IGET_UNTRUSTED means that an invalid inode number is just
132+
* fine and not an indication of a corrupted filesystem as clients can
133+
* send invalid file handles and we have to handle it gracefully..
135134
*/
136-
error = xfs_iget(mp, NULL, ino, XFS_IGET_BULKSTAT,
137-
XFS_ILOCK_SHARED, &ip, 0);
135+
error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED,
136+
XFS_ILOCK_SHARED, &ip);
138137
if (error) {
139138
/*
140139
* EINVAL means the inode cluster doesn't exist anymore.

‎fs/xfs/linux-2.6/xfs_ioctl.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,9 @@ xfs_ioc_bulkstat(
679679
error = xfs_bulkstat_single(mp, &inlast,
680680
bulkreq.ubuffer, &done);
681681
else /* XFS_IOC_FSBULKSTAT */
682-
error = xfs_bulkstat(mp, &inlast, &count,
683-
(bulkstat_one_pf)xfs_bulkstat_one, NULL,
684-
sizeof(xfs_bstat_t), bulkreq.ubuffer,
685-
BULKSTAT_FG_QUICK, &done);
682+
error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
683+
sizeof(xfs_bstat_t), bulkreq.ubuffer,
684+
&done);
686685

687686
if (error)
688687
return -error;

‎fs/xfs/linux-2.6/xfs_ioctl32.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,12 @@ xfs_bulkstat_one_compat(
237237
xfs_ino_t ino, /* inode number to get data for */
238238
void __user *buffer, /* buffer to place output in */
239239
int ubsize, /* size of buffer */
240-
void *private_data, /* my private data */
241-
xfs_daddr_t bno, /* starting bno of inode cluster */
242240
int *ubused, /* bytes used by me */
243-
void *dibuff, /* on-disk inode buffer */
244241
int *stat) /* BULKSTAT_RV_... */
245242
{
246243
return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
247-
xfs_bulkstat_one_fmt_compat, bno,
248-
ubused, dibuff, stat);
244+
xfs_bulkstat_one_fmt_compat,
245+
ubused, stat);
249246
}
250247

251248
/* copied from xfs_ioctl.c */
@@ -298,13 +295,11 @@ xfs_compat_ioc_bulkstat(
298295
int res;
299296

300297
error = xfs_bulkstat_one_compat(mp, inlast, bulkreq.ubuffer,
301-
sizeof(compat_xfs_bstat_t),
302-
NULL, 0, NULL, NULL, &res);
298+
sizeof(compat_xfs_bstat_t), 0, &res);
303299
} else if (cmd == XFS_IOC_FSBULKSTAT_32) {
304300
error = xfs_bulkstat(mp, &inlast, &count,
305-
xfs_bulkstat_one_compat, NULL,
306-
sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
307-
BULKSTAT_FG_QUICK, &done);
301+
xfs_bulkstat_one_compat, sizeof(compat_xfs_bstat_t),
302+
bulkreq.ubuffer, &done);
308303
} else
309304
error = XFS_ERROR(EINVAL);
310305
if (error)

‎fs/xfs/quota/xfs_qm.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,10 +1632,7 @@ xfs_qm_dqusage_adjust(
16321632
xfs_ino_t ino, /* inode number to get data for */
16331633
void __user *buffer, /* not used */
16341634
int ubsize, /* not used */
1635-
void *private_data, /* not used */
1636-
xfs_daddr_t bno, /* starting block of inode cluster */
16371635
int *ubused, /* not used */
1638-
void *dip, /* on-disk inode pointer (not used) */
16391636
int *res) /* result code value */
16401637
{
16411638
xfs_inode_t *ip;
@@ -1660,7 +1657,7 @@ xfs_qm_dqusage_adjust(
16601657
* the case in all other instances. It's OK that we do this because
16611658
* quotacheck is done only at mount time.
16621659
*/
1663-
if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
1660+
if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip))) {
16641661
*res = BULKSTAT_RV_NOTHING;
16651662
return error;
16661663
}
@@ -1796,12 +1793,13 @@ xfs_qm_quotacheck(
17961793
* Iterate thru all the inodes in the file system,
17971794
* adjusting the corresponding dquot counters in core.
17981795
*/
1799-
if ((error = xfs_bulkstat(mp, &lastino, &count,
1800-
xfs_qm_dqusage_adjust, NULL,
1801-
structsz, NULL, BULKSTAT_FG_IGET, &done)))
1796+
error = xfs_bulkstat(mp, &lastino, &count,
1797+
xfs_qm_dqusage_adjust,
1798+
structsz, NULL, &done);
1799+
if (error)
18021800
break;
18031801

1804-
} while (! done);
1802+
} while (!done);
18051803

18061804
/*
18071805
* We've made all the changes that we need to make incore.
@@ -1889,14 +1887,14 @@ xfs_qm_init_quotainos(
18891887
mp->m_sb.sb_uquotino != NULLFSINO) {
18901888
ASSERT(mp->m_sb.sb_uquotino > 0);
18911889
if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1892-
0, 0, &uip, 0)))
1890+
0, 0, &uip)))
18931891
return XFS_ERROR(error);
18941892
}
18951893
if (XFS_IS_OQUOTA_ON(mp) &&
18961894
mp->m_sb.sb_gquotino != NULLFSINO) {
18971895
ASSERT(mp->m_sb.sb_gquotino > 0);
18981896
if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1899-
0, 0, &gip, 0))) {
1897+
0, 0, &gip))) {
19001898
if (uip)
19011899
IRELE(uip);
19021900
return XFS_ERROR(error);

‎fs/xfs/quota/xfs_qm_syscalls.c‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ xfs_qm_scall_trunc_qfiles(
262262
}
263263

264264
if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
265-
error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
265+
error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip);
266266
if (!error) {
267267
error = xfs_truncate_file(mp, qip);
268268
IRELE(qip);
@@ -271,7 +271,7 @@ xfs_qm_scall_trunc_qfiles(
271271

272272
if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
273273
mp->m_sb.sb_gquotino != NULLFSINO) {
274-
error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
274+
error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip);
275275
if (!error2) {
276276
error2 = xfs_truncate_file(mp, qip);
277277
IRELE(qip);
@@ -417,12 +417,12 @@ xfs_qm_scall_getqstat(
417417
}
418418
if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
419419
if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
420-
0, 0, &uip, 0) == 0)
420+
0, 0, &uip) == 0)
421421
tempuqip = B_TRUE;
422422
}
423423
if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
424424
if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
425-
0, 0, &gip, 0) == 0)
425+
0, 0, &gip) == 0)
426426
tempgqip = B_TRUE;
427427
}
428428
if (uip) {
@@ -1109,10 +1109,7 @@ xfs_qm_internalqcheck_adjust(
11091109
xfs_ino_t ino, /* inode number to get data for */
11101110
void __user *buffer, /* not used */
11111111
int ubsize, /* not used */
1112-
void *private_data, /* not used */
1113-
xfs_daddr_t bno, /* starting block of inode cluster */
11141112
int *ubused, /* not used */
1115-
void *dip, /* not used */
11161113
int *res) /* bulkstat result code */
11171114
{
11181115
xfs_inode_t *ip;
@@ -1134,7 +1131,7 @@ xfs_qm_internalqcheck_adjust(
11341131
ipreleased = B_FALSE;
11351132
again:
11361133
lock_flags = XFS_ILOCK_SHARED;
1137-
if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1134+
if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip))) {
11381135
*res = BULKSTAT_RV_NOTHING;
11391136
return (error);
11401137
}
@@ -1205,15 +1202,15 @@ xfs_qm_internalqcheck(
12051202
* Iterate thru all the inodes in the file system,
12061203
* adjusting the corresponding dquot counters
12071204
*/
1208-
if ((error = xfs_bulkstat(mp, &lastino, &count,
1209-
xfs_qm_internalqcheck_adjust, NULL,
1210-
0, NULL, BULKSTAT_FG_IGET, &done))) {
1205+
error = xfs_bulkstat(mp, &lastino, &count,
1206+
xfs_qm_internalqcheck_adjust,
1207+
0, NULL, &done);
1208+
if (error) {
1209+
cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
12111210
break;
12121211
}
1213-
} while (! done);
1214-
if (error) {
1215-
cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1216-
}
1212+
} while (!done);
1213+
12171214
cmn_err(CE_DEBUG, "Checking results against system dquots");
12181215
for (i = 0; i < qmtest_hashmask; i++) {
12191216
xfs_dqtest_t *d, *n;

‎fs/xfs/xfs_dfrag.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ xfs_swapext(
6969
goto out;
7070
}
7171

72-
if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) {
72+
if (!(file->f_mode & FMODE_WRITE) ||
73+
!(file->f_mode & FMODE_READ) ||
74+
(file->f_flags & O_APPEND)) {
7375
error = XFS_ERROR(EBADF);
7476
goto out_put_file;
7577
}
@@ -81,6 +83,7 @@ xfs_swapext(
8183
}
8284

8385
if (!(tmp_file->f_mode & FMODE_WRITE) ||
86+
!(tmp_file->f_mode & FMODE_READ) ||
8487
(tmp_file->f_flags & O_APPEND)) {
8588
error = XFS_ERROR(EBADF);
8689
goto out_put_tmp_file;

‎fs/xfs/xfs_ialloc.c‎

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,63 @@ xfs_difree(
12031203
return error;
12041204
}
12051205

1206+
STATIC int
1207+
xfs_imap_lookup(
1208+
struct xfs_mount *mp,
1209+
struct xfs_trans *tp,
1210+
xfs_agnumber_t agno,
1211+
xfs_agino_t agino,
1212+
xfs_agblock_t agbno,
1213+
xfs_agblock_t *chunk_agbno,
1214+
xfs_agblock_t *offset_agbno,
1215+
int flags)
1216+
{
1217+
struct xfs_inobt_rec_incore rec;
1218+
struct xfs_btree_cur *cur;
1219+
struct xfs_buf *agbp;
1220+
xfs_agino_t startino;
1221+
int error;
1222+
int i;
1223+
1224+
error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1225+
if (error) {
1226+
xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1227+
"xfs_ialloc_read_agi() returned "
1228+
"error %d, agno %d",
1229+
error, agno);
1230+
return error;
1231+
}
1232+
1233+
/*
1234+
* derive and lookup the exact inode record for the given agino. If the
1235+
* record cannot be found, then it's an invalid inode number and we
1236+
* should abort.
1237+
*/
1238+
cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1239+
startino = agino & ~(XFS_IALLOC_INODES(mp) - 1);
1240+
error = xfs_inobt_lookup(cur, startino, XFS_LOOKUP_EQ, &i);
1241+
if (!error) {
1242+
if (i)
1243+
error = xfs_inobt_get_rec(cur, &rec, &i);
1244+
if (!error && i == 0)
1245+
error = EINVAL;
1246+
}
1247+
1248+
xfs_trans_brelse(tp, agbp);
1249+
xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1250+
if (error)
1251+
return error;
1252+
1253+
/* for untrusted inodes check it is allocated first */
1254+
if ((flags & XFS_IGET_UNTRUSTED) &&
1255+
(rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1256+
return EINVAL;
1257+
1258+
*chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1259+
*offset_agbno = agbno - *chunk_agbno;
1260+
return 0;
1261+
}
1262+
12061263
/*
12071264
* Return the location of the inode in imap, for mapping it into a buffer.
12081265
*/
@@ -1235,8 +1292,11 @@ xfs_imap(
12351292
if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
12361293
ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
12371294
#ifdef DEBUG
1238-
/* no diagnostics for bulkstat, ino comes from userspace */
1239-
if (flags & XFS_IGET_BULKSTAT)
1295+
/*
1296+
* Don't output diagnostic information for untrusted inodes
1297+
* as they can be invalid without implying corruption.
1298+
*/
1299+
if (flags & XFS_IGET_UNTRUSTED)
12401300
return XFS_ERROR(EINVAL);
12411301
if (agno >= mp->m_sb.sb_agcount) {
12421302
xfs_fs_cmn_err(CE_ALERT, mp,
@@ -1263,6 +1323,23 @@ xfs_imap(
12631323
return XFS_ERROR(EINVAL);
12641324
}
12651325

1326+
blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1327+
1328+
/*
1329+
* For bulkstat and handle lookups, we have an untrusted inode number
1330+
* that we have to verify is valid. We cannot do this just by reading
1331+
* the inode buffer as it may have been unlinked and removed leaving
1332+
* inodes in stale state on disk. Hence we have to do a btree lookup
1333+
* in all cases where an untrusted inode number is passed.
1334+
*/
1335+
if (flags & XFS_IGET_UNTRUSTED) {
1336+
error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1337+
&chunk_agbno, &offset_agbno, flags);
1338+
if (error)
1339+
return error;
1340+
goto out_map;
1341+
}
1342+
12661343
/*
12671344
* If the inode cluster size is the same as the blocksize or
12681345
* smaller we get to the buffer by simple arithmetics.
@@ -1277,24 +1354,6 @@ xfs_imap(
12771354
return 0;
12781355
}
12791356

1280-
blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1281-
1282-
/*
1283-
* If we get a block number passed from bulkstat we can use it to
1284-
* find the buffer easily.
1285-
*/
1286-
if (imap->im_blkno) {
1287-
offset = XFS_INO_TO_OFFSET(mp, ino);
1288-
ASSERT(offset < mp->m_sb.sb_inopblock);
1289-
1290-
cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
1291-
offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1292-
1293-
imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1294-
imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1295-
return 0;
1296-
}
1297-
12981357
/*
12991358
* If the inode chunks are aligned then use simple maths to
13001359
* find the location. Otherwise we have to do a btree
@@ -1304,50 +1363,13 @@ xfs_imap(
13041363
offset_agbno = agbno & mp->m_inoalign_mask;
13051364
chunk_agbno = agbno - offset_agbno;
13061365
} else {
1307-
xfs_btree_cur_t *cur; /* inode btree cursor */
1308-
xfs_inobt_rec_incore_t chunk_rec;
1309-
xfs_buf_t *agbp; /* agi buffer */
1310-
int i; /* temp state */
1311-
1312-
error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1313-
if (error) {
1314-
xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1315-
"xfs_ialloc_read_agi() returned "
1316-
"error %d, agno %d",
1317-
error, agno);
1318-
return error;
1319-
}
1320-
1321-
cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1322-
error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
1323-
if (error) {
1324-
xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1325-
"xfs_inobt_lookup() failed");
1326-
goto error0;
1327-
}
1328-
1329-
error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
1330-
if (error) {
1331-
xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1332-
"xfs_inobt_get_rec() failed");
1333-
goto error0;
1334-
}
1335-
if (i == 0) {
1336-
#ifdef DEBUG
1337-
xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1338-
"xfs_inobt_get_rec() failed");
1339-
#endif /* DEBUG */
1340-
error = XFS_ERROR(EINVAL);
1341-
}
1342-
error0:
1343-
xfs_trans_brelse(tp, agbp);
1344-
xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1366+
error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1367+
&chunk_agbno, &offset_agbno, flags);
13451368
if (error)
13461369
return error;
1347-
chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
1348-
offset_agbno = agbno - chunk_agbno;
13491370
}
13501371

1372+
out_map:
13511373
ASSERT(agbno >= chunk_agbno);
13521374
cluster_agbno = chunk_agbno +
13531375
((offset_agbno / blks_per_cluster) * blks_per_cluster);

0 commit comments

Comments
 (0)