Annotation of truecrypt/linux/kernel/dm-target.c, revision 1.1.1.4

1.1       root        1: /* 
1.1.1.3   root        2: Copyright (c) 2004-2006 TrueCrypt Foundation. All rights reserved. 
1.1       root        3: 
1.1.1.4 ! root        4: Covered by TrueCrypt License 2.1 the full text of which is contained in the file
1.1       root        5: License.txt included in TrueCrypt binary and source code distribution archives. 
                      6: */
                      7: 
                      8: #include <linux/bio.h>
1.1.1.4 ! root        9: #include <linux/blkdev.h>
1.1       root       10: #include <linux/ctype.h>
1.1.1.4 ! root       11: #include <linux/device-mapper.h>
1.1       root       12: #include <linux/init.h>
1.1.1.4 ! root       13: #include <linux/kdev_t.h>
1.1       root       14: #include <linux/module.h>
                     15: #include <linux/moduleparam.h>
                     16: #include <linux/version.h>
                     17: #include <linux/workqueue.h>
1.1.1.4 ! root       18: #include <dm.h>
1.1       root       19: 
                     20: #include "Tcdefs.h"
                     21: #include "Crypto.h"
                     22: #include "Tests.h"
                     23: #include "Dm-target.h"
                     24: 
                     25: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
                     26: #error Linux kernel 2.6.5 or later required
                     27: #endif
                     28: 
                     29: int trace_level = 0;
                     30: 
                     31: #define MSG_PREFIX "truecrypt: "
                     32: #define error(fmt, args...) printk(KERN_ERR MSG_PREFIX fmt, ## args)
                     33: #define trace(level, fmt, args...) level <= trace_level && printk(KERN_DEBUG MSG_PREFIX fmt, ## args)
                     34: #define dbg(fmt, args...) printk(KERN_DEBUG MSG_PREFIX fmt, ## args)
                     35: 
                     36: #define MIN_POOL_SIZE 16
                     37: 
1.1.1.4 ! root       38: #ifndef __GFP_NOMEMALLOC
        !            39: #define __GFP_NOMEMALLOC 0
        !            40: #endif
        !            41: 
1.1       root       42: struct target_ctx
                     43: {
                     44:        struct dm_dev *dev;
                     45:        sector_t start;
                     46:        char *volume_path;
                     47:        mempool_t *bio_ctx_pool;
                     48:        mempool_t *pg_pool;
1.1.1.4 ! root       49:        unsigned long long read_only_start;
        !            50:        unsigned long long read_only_end;
        !            51:        unsigned long long mtime;
        !            52:        unsigned long long atime;
1.1       root       53:        int flags;
1.1.1.2   root       54:        PCRYPTO_INFO ci;
1.1       root       55: };
                     56: 
                     57: struct bio_ctx
                     58: {
                     59:        struct dm_target *target;
                     60:        struct bio *orig_bio;
                     61:        atomic_t ref_count;
                     62:        u64 crypto_sector;
                     63:        int error;
                     64:        struct work_struct work;
                     65: };
                     66: 
                     67: static struct workqueue_struct *work_queue = NULL;
                     68: static kmem_cache_t *bio_ctx_cache = NULL;
                     69: 
1.1.1.4 ! root       70: #define READ_ONLY(tc) (tc->flags & TC_READ_ONLY)
        !            71: #define HID_VOL_PROT(tc) (tc->flags & TC_HIDDEN_VOLUME_PROTECTION)
1.1       root       72: 
                     73: 
                     74: static int hex2bin (char *hex_string, u8 *byte_buf, int max_length)
                     75: {
                     76:        int i = 0, n;
                     77:        char s[3];
                     78:        s[2] = 0;
                     79: 
                     80:        while (i < max_length
                     81:                && (s[0] = *hex_string++) 
                     82:                && (s[1] = *hex_string++))
                     83:        {
                     84:                if (sscanf (s, "%x", &n) != 1)
                     85:                        return 0;
                     86:                byte_buf[i++] = (u8) n;
                     87:        }
                     88: 
                     89:        return i;
                     90: }
                     91: 
1.1.1.4 ! root       92: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
        !            93: 
        !            94: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
        !            95: static void *mempool_alloc_pg (gfp_t gfp_mask, void *pool_data)
        !            96: #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1.1       root       97: static void *mempool_alloc_pg (unsigned int gfp_mask, void *pool_data)
                     98: #else
                     99: static void *mempool_alloc_pg (int gfp_mask, void *pool_data)
                    100: #endif
                    101: {
                    102:        trace (3, "mempool_alloc_pg (%d, %p)\n", gfp_mask, pool_data);
                    103:        return alloc_page (gfp_mask);
                    104: }
                    105: 
                    106: static void mempool_free_pg (void *element, void *pool_data)
                    107: {
                    108:        trace (3, "mempool_free_pg (%p, %p)\n", element, pool_data);
                    109:        __free_page (element);
                    110: }
                    111: 
1.1.1.4 ! root      112: #endif // LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
        !           113: 
        !           114: 
        !           115: static void *malloc_wait (mempool_t *pool, int direction)
        !           116: {
        !           117:        void *p;
        !           118:        trace (3, "malloc_wait\n");
        !           119: 
        !           120:        while (1)
        !           121:        {
        !           122:                p = mempool_alloc (pool, GFP_NOIO | __GFP_NOMEMALLOC);
        !           123: 
        !           124:                if (p)
        !           125:                        return p;
        !           126: 
        !           127:                trace (3, "blk_congestion_wait\n");
        !           128:                blk_congestion_wait (direction, HZ / 50);
        !           129:        }
        !           130: }
        !           131: 
1.1       root      132: 
1.1.1.2   root      133: static void wipe_args (unsigned int argc, char **argv)
                    134: {
                    135:        int i;
                    136:        for (i = 0; i < argc; i++)
                    137:        {
                    138:                if (argv[i] != NULL)
                    139:                        burn (argv[i], strlen (argv[i]));
                    140:        }
                    141: }
                    142: 
                    143: 
1.1       root      144: static int truecrypt_ctr (struct dm_target *ti, unsigned int argc, char **argv)
                    145: {
                    146:        struct target_ctx *tc;
                    147:        int key_size;
                    148:        int error = -EINVAL;
1.1.1.4 ! root      149:        unsigned long long sector;
1.1       root      150: 
                    151:        trace (3, "truecrypt_ctr (%p, %d, %p)\n", ti, argc, argv);
                    152: 
1.1.1.4 ! root      153:        if (argc != TC_LAST_ARG + 1)
1.1       root      154:        {
1.1.1.2   root      155:                ti->error = "truecrypt: Usage: <start_sector> <sector_count> truecrypt <EA> <mode> <key> <key2/IV> <host_device> <sector_offset> <read_only_start> <read_only_end> <mtime> <atime> <flags> <volume_path>";
1.1       root      156:                return -EINVAL;
                    157:        }
                    158: 
                    159:        tc = kmalloc (sizeof (*tc), GFP_KERNEL);
                    160:        if (tc == NULL)
                    161:        {
                    162:                ti->error = "truecrypt: Cannot allocate target context";
                    163:                error = -ENOMEM;
                    164:                goto err;
                    165:        }
                    166:        memset (tc, 0, sizeof (*tc));
                    167: 
1.1.1.2   root      168:        tc->ci = crypto_open ();
                    169:        if (tc == NULL)
                    170:        {
                    171:                ti->error = "truecrypt: Cannot allocate crypto_info";
                    172:                error = -ENOMEM;
                    173:                goto err;
                    174:        }
                    175: 
1.1.1.4 ! root      176: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
        !           177:        tc->bio_ctx_pool = mempool_create_slab_pool (MIN_POOL_SIZE, bio_ctx_cache);
        !           178: #else
1.1       root      179:        tc->bio_ctx_pool = mempool_create (MIN_POOL_SIZE, mempool_alloc_slab, mempool_free_slab, bio_ctx_cache);
1.1.1.4 ! root      180: #endif
        !           181: 
1.1       root      182:        if (!tc->bio_ctx_pool)
                    183:        {
                    184:                ti->error = "truecrypt: Cannot create bio context memory pool";
                    185:                error = -ENOMEM;
                    186:                goto err;
                    187:        }
                    188: 
1.1.1.4 ! root      189: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
        !           190:        tc->pg_pool = mempool_create_page_pool (MIN_POOL_SIZE, 0);
        !           191: #else
1.1       root      192:        tc->pg_pool = mempool_create (MIN_POOL_SIZE, mempool_alloc_pg, mempool_free_pg, NULL);
1.1.1.4 ! root      193: #endif
1.1       root      194:        if (!tc->pg_pool)
                    195:        {
                    196:                ti->error = "truecrypt: Cannot create page memory pool";
                    197:                error = -ENOMEM;
                    198:                goto err;
                    199:        }
                    200: 
1.1.1.4 ! root      201:        if (sscanf (argv[TC_ARG_SEC], "%llu", &sector) != 1)
1.1       root      202:        {
                    203:                ti->error = "truecrypt: Invalid device sector";
                    204:                goto err;
                    205:        }
1.1.1.4 ! root      206:        tc->start = sector;
1.1       root      207: 
1.1.1.4 ! root      208:        if (dm_get_device (ti, argv[TC_ARG_DEV], tc->start, ti->len, dm_table_get_mode (ti->table), &tc->dev))
1.1       root      209:        {
                    210:                ti->error = "truecrypt: Device lookup failed";
                    211:                goto err;
                    212:        }
                    213: 
                    214:        // Encryption algorithm
1.1.1.2   root      215:        tc->ci->ea = 0;
1.1.1.4 ! root      216:        if (sscanf (argv[TC_ARG_EA], "%d", &tc->ci->ea) != 1
1.1.1.2   root      217:                || tc->ci->ea < EAGetFirst ()
                    218:                || tc->ci->ea > EAGetCount ())
1.1       root      219:        {
                    220:                ti->error = "truecrypt: Invalid encryption algorithm";
                    221:                goto err;
                    222:        }
                    223: 
1.1.1.2   root      224:        // Mode of operation
                    225:        tc->ci->mode = 0;
1.1.1.4 ! root      226:        if (sscanf (argv[TC_ARG_MODE], "%d", &tc->ci->mode) != 1
1.1.1.2   root      227:                || tc->ci->mode < 1
                    228:                || tc->ci->mode >= INVALID_MODE)
1.1       root      229:        {
1.1.1.2   root      230:                ti->error = "truecrypt: Invalid mode of operation";
1.1       root      231:                goto err;
                    232:        }
                    233: 
1.1.1.2   root      234:        // Key
                    235:        key_size = EAGetKeySize (tc->ci->ea);
1.1.1.4 ! root      236:        if (hex2bin (argv[TC_ARG_KEY], tc->ci->master_key, key_size) != key_size)
1.1       root      237:        {
                    238:                ti->error = "truecrypt: Invalid key";
                    239:                goto err;
                    240:        }
1.1.1.2   root      241:        
                    242:        // EA init
                    243:        trace (2, "EAInit (%d, %p, %p)\n", tc->ci->ea, tc->ci->master_key, tc->ci->ks);
                    244:        if (EAInit (tc->ci->ea, tc->ci->master_key, tc->ci->ks) == ERR_CIPHER_INIT_FAILURE)
1.1       root      245:        {
1.1.1.2   root      246:                ti->error = "truecrypt: Encryption algorithm initialization failed";
1.1       root      247:                goto err;
                    248:        }
                    249: 
1.1.1.2   root      250:        // Key2 / IV
1.1.1.4 ! root      251:        if (hex2bin (argv[TC_ARG_IV], tc->ci->iv, sizeof (tc->ci->iv)) != sizeof (tc->ci->iv))
1.1       root      252:        {
                    253:                ti->error = "truecrypt: Invalid IV";
                    254:                goto err;
                    255:        }
                    256: 
1.1.1.2   root      257:        // Mode init    
                    258:        if (!EAInitMode (tc->ci))
                    259:        {
                    260:                ti->error = "truecrypt: Mode of operation initialization failed";
                    261:                goto err;
                    262:        }
                    263: 
1.1       root      264:        // Read-only start sector
1.1.1.4 ! root      265:        if (sscanf (argv[TC_ARG_RO_START], "%llu", &tc->read_only_start) != 1)
1.1       root      266:        {
                    267:                ti->error = "truecrypt: Invalid read-only start sector";
                    268:                goto err;
                    269:        }
                    270: 
                    271:        // Read-only end sector
1.1.1.4 ! root      272:        if (sscanf (argv[TC_ARG_RO_END], "%llu", &tc->read_only_end) != 1)
1.1       root      273:        {
                    274:                ti->error = "truecrypt: Invalid read-only end sector";
                    275:                goto err;
                    276:        }
                    277: 
                    278:        // Modification time
1.1.1.4 ! root      279:        if (sscanf (argv[TC_ARG_MTIME], "%llu", &tc->mtime) != 1)
1.1       root      280:        {
                    281:                ti->error = "truecrypt: Invalid modification time";
                    282:                goto err;
                    283:        }
                    284: 
                    285:        // Access time
1.1.1.4 ! root      286:        if (sscanf (argv[TC_ARG_ATIME], "%llu", &tc->atime) != 1)
1.1       root      287:        {
                    288:                ti->error = "truecrypt: Invalid access time";
                    289:                goto err;
                    290:        }
                    291: 
                    292:        // Flags
1.1.1.4 ! root      293:        if (sscanf (argv[TC_ARG_FLAGS], "%d", &tc->flags) != 1)
1.1       root      294:        {
                    295:                ti->error = "truecrypt: Invalid flags";
                    296:                goto err;
                    297:        }
                    298: 
                    299:        // Volume path
1.1.1.4 ! root      300:        tc->volume_path = kmalloc (strlen (argv[TC_ARG_VOL]) + 1, GFP_KERNEL);
1.1       root      301:        if (tc->volume_path == NULL)
                    302:        {
                    303:                ti->error = "truecrypt: Cannot allocate volume path buffer";
                    304:                error = -ENOMEM;
                    305:                goto err;
                    306:        }
1.1.1.4 ! root      307:        strcpy (tc->volume_path, argv[TC_ARG_VOL]);
1.1       root      308: 
1.1.1.2   root      309:        // Hidden volume
                    310:        if (tc->start > 1)
                    311:        {
                    312:                tc->ci->hiddenVolume = TRUE;
                    313:                tc->ci->hiddenVolumeOffset = tc->start * SECTOR_SIZE;
                    314:        }
                    315: 
1.1       root      316:        ti->private = tc;
1.1.1.2   root      317: 
                    318:        wipe_args (argc, argv);
1.1       root      319:        return 0;
                    320: 
                    321: err:
                    322:        if (tc)
                    323:        {
1.1.1.2   root      324:                if (tc->ci)
                    325:                        crypto_close (tc->ci);
1.1       root      326:                if (tc->volume_path)
                    327:                        kfree (tc->volume_path);
                    328:                if (tc->bio_ctx_pool)
                    329:                        mempool_destroy (tc->bio_ctx_pool);
                    330:                if (tc->pg_pool)
                    331:                        mempool_destroy (tc->pg_pool);
                    332:                kfree (tc);
                    333:        }
                    334: 
1.1.1.2   root      335:        wipe_args (argc, argv);
1.1       root      336:        return error;
                    337: }
                    338: 
                    339: 
                    340: static void truecrypt_dtr (struct dm_target *ti)
                    341: {
                    342:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    343: 
                    344:        trace (3, "truecrypt_dtr (%p)\n", ti);
                    345: 
                    346:        mempool_destroy (tc->bio_ctx_pool);
1.1.1.2   root      347:        mempool_destroy (tc->pg_pool);
                    348:        crypto_close (tc->ci);
1.1       root      349:        kfree(tc->volume_path);
1.1.1.2   root      350:        dm_put_device(ti, tc->dev);
1.1       root      351:        kfree(tc);
                    352: }
                    353: 
                    354: 
                    355: // Checks if two regions overlap (borders are parts of regions)
1.1.1.4 ! root      356: static int RegionsOverlap (u64 start1, u64 end1, u64 start2, u64 end2)
1.1       root      357: {
                    358:        return (start1 < start2) ? (end1 >= start2) : (start1 <= end2);
                    359: }
                    360: 
                    361: 
                    362: static void dereference_bio_ctx (struct bio_ctx *bc)
                    363: {
                    364:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    365:        trace (3, "dereference_bio_ctx (%p)\n", bc);
                    366: 
                    367:        if (!atomic_dec_and_test (&bc->ref_count))
                    368:                return;
                    369: 
                    370:        bio_endio (bc->orig_bio, bc->orig_bio->bi_size, bc->error);
1.1.1.4 ! root      371:        trace (3, "deref: mempool_free (%p)\n", bc);
1.1       root      372:        mempool_free (bc, tc->bio_ctx_pool);
                    373: }
                    374: 
                    375: 
1.1.1.4 ! root      376: static void work_process (void *qdata)
1.1       root      377: {
1.1.1.4 ! root      378:        struct bio_ctx *bc = (struct bio_ctx *) qdata;
1.1       root      379:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    380:        struct bio_vec *bv;
1.1.1.4 ! root      381:        u64 sec_no = bc->crypto_sector;
1.1       root      382:        int seg_no;
                    383:        unsigned long flags;
                    384: 
1.1.1.4 ! root      385:        trace (3, "work_process (%p)\n", qdata);
1.1       root      386: 
                    387:        // Decrypt queued data
                    388:        bio_for_each_segment (bv, bc->orig_bio, seg_no)
                    389:        {
                    390:                unsigned int secs = bv->bv_len / SECTOR_SIZE;
                    391:                char *data = bvec_kmap_irq (bv, &flags);
                    392: 
1.1.1.4 ! root      393:                trace (2, "DecryptSectors (%llu, %d)\n", (unsigned long long) sec_no, secs);
1.1.1.2   root      394:                DecryptSectors ((unsigned __int32 *)data, sec_no, secs, tc->ci);
1.1       root      395: 
                    396:                sec_no += secs;
                    397: 
                    398:                flush_dcache_page (bv->bv_page);
                    399:                bvec_kunmap_irq (data, &flags);
1.1.1.4 ! root      400: 
        !           401:                if (seg_no + 1 < bc->orig_bio->bi_vcnt)
        !           402:                        cond_resched ();
1.1       root      403:        }
                    404: 
                    405:        dereference_bio_ctx (bc);
                    406: }
                    407: 
                    408: 
                    409: static int truecrypt_endio (struct bio *bio, unsigned int bytes_done, int error)
                    410: {
                    411:        struct bio_ctx *bc = (struct bio_ctx *) bio->bi_private;
                    412:        struct target_ctx *tc = (struct target_ctx *) bc->target->private;
                    413:        struct bio_vec *bv;
                    414:        int seg_no;
                    415:        
                    416:        trace (3, "truecrypt_endio (%p, %d, %d)\n", bio, bytes_done, error);
1.1.1.4 ! root      417:        trace (1, "end: sc=%llu fl=%ld rw=%ld sz=%d ix=%hd vc=%hd dn=%d er=%d\n",
        !           418:                (unsigned long long) bio->bi_sector, bio->bi_flags, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt, bytes_done, error);
1.1       root      419: 
                    420:        if (error != 0)
                    421:                bc->error = error;
                    422: 
                    423:        if (bio->bi_size)
                    424:        {
                    425:                trace (2, "Outstanding IO: %d\n", bio->bi_size);
                    426:                return 1;
                    427:        }
                    428: 
                    429:        if (bio_data_dir (bio) == READ)
                    430:        {
                    431:                bio_put (bio);
                    432: 
                    433:                // Queue decryption to leave completion interrupt ASAP
                    434:                INIT_WORK (&bc->work, work_process, bc);
                    435:                trace (3, "queue_work (%p)\n", work_queue);
                    436:                queue_work (work_queue, &bc->work);
                    437:                return error;
                    438:        }
                    439: 
                    440:        // Free pages allocated for encryption
                    441:        bio_for_each_segment (bv, bio, seg_no)
                    442:        {
1.1.1.4 ! root      443:                trace (3, "endio: mempool_free (%p)\n", bv->bv_page);
1.1       root      444:                mempool_free (bv->bv_page, tc->pg_pool);  
                    445:        }
                    446: 
                    447:        bio_put (bio);
                    448:        dereference_bio_ctx (bc);
                    449:        return error;
                    450: }
                    451: 
                    452: 
                    453: static int truecrypt_map (struct dm_target *ti, struct bio *bio, union map_info *map_context)
                    454: {
                    455:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    456:        struct bio_ctx *bc;
                    457:        struct bio *bion;
                    458:        struct bio_vec *bv;
                    459:        int seg_no;
                    460: 
                    461:        trace (3, "truecrypt_map (%p, %p, %p)\n", ti, bio, map_context);
1.1.1.4 ! root      462:        trace (1, "map: sc=%llu fl=%ld rw=%ld sz=%d ix=%hd vc=%hd\n",
        !           463:                (unsigned long long) bio->bi_sector, bio->bi_flags, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt);
1.1       root      464: 
                    465:        // Write protection
                    466:        if (bio_data_dir (bio) == WRITE && READ_ONLY (tc))
                    467:                return -EPERM;
                    468: 
                    469:        // Validate segment sizes
                    470:        bio_for_each_segment (bv, bio, seg_no)
                    471:        {
                    472:                if (bv->bv_len & (SECTOR_SIZE - 1))
                    473:                {
1.1.1.4 ! root      474:                        error ("unsupported bio segment size %d (%ld %d %hd %hd)\n",
1.1       root      475:                                bv->bv_len, bio->bi_rw, bio->bi_size, bio->bi_idx, bio->bi_vcnt);
                    476:                        return -EINVAL;
                    477:                }
                    478:        }
                    479: 
                    480:        // Bio context
1.1.1.4 ! root      481:        bc = malloc_wait (tc->bio_ctx_pool, bio_data_dir (bio));
1.1       root      482:        if (!bc)
                    483:        {
                    484:                error ("bio context allocation failed\n");
                    485:                return -ENOMEM;
                    486:        }
1.1.1.4 ! root      487:        trace (3, "mempool_alloc bc: %p\n", bc);
1.1       root      488: 
                    489:        atomic_set (&bc->ref_count, 1);
                    490:        bc->orig_bio = bio;
                    491:        bc->error = 0;
                    492:        bc->target = ti;
                    493:        bc->crypto_sector = tc->start + (bio->bi_sector - ti->begin);
                    494: 
                    495:        // New bio for encrypted device
                    496:        trace (3, "bio_alloc (%hd)\n", bio_segments (bio));
1.1.1.4 ! root      497:        while (!(bion = bio_alloc (GFP_NOIO | __GFP_NOMEMALLOC, bio_segments (bio))))
1.1       root      498:        {
1.1.1.4 ! root      499:                trace (3, "blk_congestion_wait\n");
        !           500:                blk_congestion_wait (bio_data_dir (bio), HZ / 50);
1.1       root      501:        }
                    502: 
                    503:        bion->bi_bdev = tc->dev->bdev;
                    504:        bion->bi_end_io = truecrypt_endio;
                    505:        bion->bi_idx = 0;
                    506:        bion->bi_private = bc;
                    507:        bion->bi_rw = bio->bi_rw;
                    508:        bion->bi_sector = bc->crypto_sector;
                    509:        bion->bi_size = bio->bi_size;
                    510:        bion->bi_vcnt = bio_segments (bio);
                    511: 
                    512:        if (bio_data_dir (bio) == READ)
                    513:        {
                    514:                // Buffers of originating bio can be used for decryption
                    515:                memcpy (bion->bi_io_vec,
                    516:                        bio_iovec (bio),
                    517:                        bion->bi_vcnt * sizeof (struct bio_vec));
                    518:        }
                    519:        else
                    520:        {
1.1.1.4 ! root      521:                u64 sec_no = bc->crypto_sector;
        !           522:                int seg_no;
        !           523: 
1.1       root      524:                // Encrypt data to be written
                    525:                unsigned long flags, copyFlags;
                    526:                char *data, *copy;
                    527: 
                    528:                memset (bion->bi_io_vec, 0, sizeof (struct bio_vec) * bion->bi_vcnt);
                    529: 
                    530:                bio_for_each_segment (bv, bio, seg_no)
                    531:                {
                    532:                        struct bio_vec *cbv = bio_iovec_idx (bion, seg_no);
                    533:                        unsigned int secs = bv->bv_len / SECTOR_SIZE;
                    534: 
                    535:                        // Hidden volume protection
                    536:                        if (!READ_ONLY (tc) && HID_VOL_PROT (tc)
                    537:                                && RegionsOverlap (sec_no, sec_no + secs - 1, tc->read_only_start, tc->read_only_end))
                    538:                        {
1.1.1.4 ! root      539:                                tc->flags |= TC_READ_ONLY | TC_PROTECTION_ACTIVATED;
1.1       root      540:                        }
                    541: 
1.1.1.4 ! root      542:                        if (READ_ONLY (tc))
1.1       root      543:                        {
1.1.1.4 ! root      544:                                // Write not permitted
1.1       root      545:                                bio_for_each_segment (cbv, bion, seg_no)
                    546:                                {
                    547:                                        if (cbv->bv_page != NULL)
                    548:                                                mempool_free (cbv->bv_page, tc->pg_pool);  
                    549:                                }
                    550: 
                    551:                                bio_put (bion);
                    552:                                bc->error = READ_ONLY (tc) ? -EPERM : -ENOMEM;
                    553:                                dereference_bio_ctx (bc);
                    554:                                return 0;
                    555:                        }
1.1.1.4 ! root      556: 
        !           557:                        cbv->bv_page = malloc_wait (tc->pg_pool, bio_data_dir (bion));
        !           558:                        trace (3, "mempool_alloc cbv: %p\n", bc);
1.1       root      559: 
                    560:                        cbv->bv_offset = 0;
                    561:                        cbv->bv_len = bv->bv_len;
                    562: 
                    563:                        copy = bvec_kmap_irq (cbv, &copyFlags);
1.1.1.3   root      564:                        data = bvec_kmap_irq (bv, &flags);
1.1       root      565: 
                    566:                        memcpy (copy, data, bv->bv_len);
                    567: 
1.1.1.4 ! root      568:                        trace (2, "EncryptSectors (%llu, %d)\n", (unsigned long long) sec_no, secs);
1.1       root      569: 
1.1.1.4 ! root      570:                        EncryptSectors ((unsigned __int32 *) copy, sec_no, secs, tc->ci);
1.1       root      571:                        sec_no += secs;
                    572: 
1.1.1.4 ! root      573:                        bvec_kunmap_irq (data, &flags);
1.1       root      574:                        bvec_kunmap_irq (copy, &copyFlags);
1.1.1.4 ! root      575:                        flush_dcache_page (bv->bv_page);
        !           576:                        flush_dcache_page (cbv->bv_page);
        !           577: 
        !           578:                        if (seg_no + 1 < bio->bi_vcnt)
        !           579:                                cond_resched();
1.1       root      580:                }
                    581:        }
                    582: 
                    583:        atomic_inc (&bc->ref_count);
                    584: 
1.1.1.4 ! root      585:        trace (3, "generic_make_request (rw=%ld sc=%llu)\n", bion->bi_rw, (unsigned long long) bion->bi_sector);
1.1       root      586:        generic_make_request (bion);
                    587: 
                    588:        dereference_bio_ctx (bc);
                    589:        return 0;
                    590: }
                    591: 
                    592: 
                    593: static int truecrypt_status (struct dm_target *ti, status_type_t type, char *result, unsigned int maxlen)
                    594: {
                    595:        struct target_ctx *tc = (struct target_ctx *) ti->private;
                    596: 
                    597:        switch (type)
                    598:        {
                    599:        case STATUSTYPE_INFO:
                    600:                result[0] = 0;
                    601:                break;
                    602: 
                    603:        case STATUSTYPE_TABLE:
                    604:                {
                    605:                        char name[32];
                    606:                        format_dev_t (name, tc->dev->bdev->bd_dev);
1.1.1.4 ! root      607:                        snprintf (result, maxlen, "%d %d 0 0 %s %llu %llu %llu %llu %llu %d %s",
1.1.1.2   root      608:                                tc->ci->ea,
                    609:                                tc->ci->mode,
1.1       root      610:                                name,
1.1.1.4 ! root      611:                                (unsigned long long) tc->start,
1.1       root      612:                                tc->read_only_start,
                    613:                                tc->read_only_end,
                    614:                                tc->mtime,
                    615:                                tc->atime,
                    616:                                tc->flags,
                    617:                                tc->volume_path);
                    618:                }
                    619:                break;
                    620:        }
                    621: 
                    622:        return 0;
                    623: }
                    624: 
                    625: 
                    626: static struct target_type truecrypt_target = {
                    627:        .name   = "truecrypt",
1.1.1.4 ! root      628:        .version= {TC_VERSION_NUM1, TC_VERSION_NUM2, TC_VERSION_NUM3},
1.1       root      629:        .module = THIS_MODULE,
                    630:        .ctr    = truecrypt_ctr,
                    631:        .dtr    = truecrypt_dtr,
                    632:        .map    = truecrypt_map,
                    633:        .status = truecrypt_status
                    634: };
                    635: 
                    636: 
                    637: int __init dm_truecrypt_init(void)
                    638: {
                    639:        int r;
                    640:        trace (3, "dm_truecrypt_init (trace_level=%d)\n", trace_level);
                    641: 
                    642:        if (!AutoTestAlgorithms ())
                    643:        {
                    644:                DMERR ("truecrypt: self-test of algorithms failed");
                    645:                return -ERANGE;
                    646:        }
                    647: 
                    648:        work_queue = create_workqueue ("truecryptq");
                    649: 
                    650:        if (!work_queue)
                    651:        {
1.1.1.4 ! root      652:                DMERR ("truecrypt: create_workqueue failed");
1.1       root      653:                goto err;
                    654:        }
                    655: 
                    656:        bio_ctx_cache = kmem_cache_create ("truecrypt-bioctx", sizeof (struct bio_ctx), 0, 0, NULL, NULL);
                    657:        if (!bio_ctx_cache)
                    658:        {
                    659:                DMERR ("truecrypt: kmem_cache_create failed");
                    660:                goto err;
                    661:        }
                    662: 
                    663:        r = dm_register_target (&truecrypt_target);
                    664:        if (r < 0)
                    665:        {
                    666:                DMERR ("truecrypt: register failed %d", r);
                    667:                goto err;
                    668:        }
                    669: 
                    670:        return r;
                    671: 
                    672: err:
                    673:        if (work_queue)
                    674:                destroy_workqueue (work_queue);
                    675:        if (bio_ctx_cache)
                    676:                kmem_cache_destroy (bio_ctx_cache);
                    677: 
                    678:        return -ENOMEM;
                    679: }
                    680: 
                    681: 
                    682: void __exit dm_truecrypt_exit(void)
                    683: {
                    684:        int r;
                    685:        trace (3, "dm_truecrypt_exit ()\n");
                    686: 
                    687:        r = dm_unregister_target (&truecrypt_target);
                    688: 
                    689:        if (r < 0)
                    690:                DMERR ("truecrypt: unregister failed %d", r);
                    691: 
                    692:        destroy_workqueue (work_queue);
                    693:        kmem_cache_destroy (bio_ctx_cache);
                    694: }
                    695: 
                    696: 
                    697: module_init(dm_truecrypt_init);
                    698: module_exit(dm_truecrypt_exit);
                    699: module_param_named(trace, trace_level, int, 0);
                    700: 
                    701: MODULE_AUTHOR("TrueCrypt Foundation");
                    702: MODULE_DESCRIPTION(DM_NAME " target for encryption and decryption of TrueCrypt volumes");
                    703: MODULE_PARM_DESC(trace, "Trace level");
                    704: MODULE_LICENSE("GPL and additional rights"); // Kernel thinks only GPL/BSD/MPL != closed-source code

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.